0

I have a socket server which keeps listening to incoming requests. The data received will be in the form of binary array of bytes. Data format is something like this. 2321902321221200AA

  • Whereas 1 byte is data begin
  • 4 bits is version
  • 4 bits is data return type
  • 5 bytes are product code
  • 2 bytes data length

The question is, how to parse the data and segregate the parameters.

Thanks in advance!!

user2058277
  • 1
  • 1
  • 1

6 Answers6

1

Try java.io.DataInputStream:

    DataInputStream dis = new DataInputStream(in);
    byte b = dis.readByte();
    int version = (b >> 4) & 0xF;
    int returnType = b & 0xF;
    byte[] productCode = new byte[5];
    dis.readFully(productCode);
    int len = dis.readShort() & 0xFFFF;
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

if use the java binary block parser then code will look like

class Parsed { 
  @Bin byte begin; 
  @Bin(type = BinType.BIT) int version; 
  @Bin(type = BinType.BIT) int returnType;
  @Bin byte [] productCode;
  @Bin(type = BinType.USHORT) int dataLength;
}
final Parsed parsed = JBBPParser.prepare("byte begin; bit:4 version; bit:4 returnType; byte [5] productCode; ushort dataLength;")
        .parse(new byte[]{0x23,0x21,(byte)0x90,0x23,0x21,0x22,0x12,0x00,(byte)0xAA})
        .mapTo(Parsed.class);

assertEquals(0x23, parsed.begin);
assertEquals(0x01, parsed.version);
assertEquals(0x02, parsed.returnType);
assertArrayEquals(new byte[]{(byte)0x90,0x23,0x21,0x22,0x12}, parsed.productCode);
assertEquals(0x00AA,parsed.dataLength);
Igor Maznitsa
  • 833
  • 7
  • 12
0
try {
    char [] cbuf = new char[16];
    char databegin = cbuf[0];
    char [] version = Arrays.copyOfRange(cbuf, 1, 6) 
    char [] product_typep = Arrays.copyOfRange(cbuf, 7, 12)
    char []data_lendth = Arrays.copyOfRange(cbuf, 13, 15)

} catch(Error e){
    System.out.println(e);
}
user000001
  • 32,226
  • 12
  • 81
  • 108
0
byte [] data = receiveData ();

int dataBegin = data [0];           // Once field is 1-byte, it is simple!
int version = data [1] & 0x0F;      // Use shift (>>>) and binary "and" (&)
int returnCode =                    // to extract value of fields that are
    (data [1] >>> 4) & 0x0F;        // smaller than one byte
byte [] productCode =               // Copy fixed-size portions of data
    new byte [] {                   // into separate arrays using hardcode
        data [2], data [3],         // (as here),  or System.arrayCopy
        data [4], data [5],         // in case field occupies quite
        data [6]};                  // a many bytes.
int dataLength =                    // Use shift (<<) binary or (|) to 
    (data [7] & 0xFF) |             // Combine several bytes into one integer
    ((data [8] & 0xFF) << 8);       // We assume little-endian encoding here
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
0

I would got for some king of package reader:

class Record {

   .....

   Record static fromBytes(byte[] bytes) {
       // here use ByteBuffer or DataInputStream to extract filds
       ......
   }
}


Record readNextRecord(InputStream in) {
    int len = in.read() && 0xFF;
    byte[] data = new byte[len];
    in.read(data);
    return Record.fromBytes(data)
}



{
  InputStream in = ....;
  Record r readNextRecord(in);
  process (r);
}

Of course you need to add error handling. In general, for something which should run reliable, I will suggest to use NIO framework like Grizzly or Netty.

kofemann
  • 4,217
  • 1
  • 34
  • 39
-1

You might get the data via the ByteArrayOutputStream And then parse the bytes by applying masks (mainly AND, OR).

Take a look at This question

Hope this helps

Community
  • 1
  • 1
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97