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!!

6 Answers 6

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;
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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);
}

Comments

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

Comments

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.

Comments

-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

1 Comment

ByteArrayOutputStream doesn't answer the question, it just moves the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.