I would like to read a line from a TCP stream that is specified with a byte length, followed by the actual data. In Python, I would do
length = ord(stream.read(1))
data = stream.read(length)
How to do the same in Java NIO? I have a buffer (capacity 257)
stream.read(buffer);
int length = buffer.get();
byte[] data = new byte[length];
buffer.get(data);
Unfortunately, this does not work: calls to get () look at the data in the buffer: - (
I probably need a combination of flip, rewind, reset, etc., but I can't figure it out.
source
share