Given the following pseudo code. how would I do read in the given data
You can try JBBP
@Bin class Struct { byte [] payload; }
@Bin class ParsedStream { Struct [] structs; }
ParsedStream parsed = JBBPParser.prepare("structs[_] { ushort size; byte [size] payload; }").parse(theInStream).mapTo(ParsedStream.class);
Use DataInputStream to make your life easy.
DataInputStream in = new DataInputStream(socket.getInputStream());
short myShortStreamSize = in.readShort();
byte[] payload = new byte[myShortStreamSize];
in.readFully(payload);
Socket
has a getInputStream()
method. You would use the returned InputStream
and read myShortStreamSize
of bytes from it into a byte[]
, convert that into a int/long representing your payload size and then read into another, larger, new byte[payloadSize]
, the payload itself.