Reputation: 15365
I have to decode a socket transmission which contains pieces of a file:
data %filekey% [3:%piece3% 5:%piece5% 7:%piece7% 8:%piece8% 9:%piece9%]
%pieceN%
contains the N-th binary piece of the file%filekey%
is known\n
).I'm facing two problems:
InputStream
, looking for \n
. But what if a byte from %pieceN%
also contains a carriage return ?N:
. Just like my previous question: what if %pieceN%
contains a :
?Upvotes: 1
Views: 201
Reputation: 183311
Since you know the lengths of every part of data %filekey% [3:%piece3% 5:%piece5% 7:%piece7% 8:%piece8% 9:%piece9%]
— you know the number of spaces, you know what %filekey%
is, you know the lengths of each %pieceN%
, etc. — this means you know the full length of data %filekey% [3:%piece3% 5:%piece5% 7:%piece7% 8:%piece8% 9:%piece9%]
, so you can just use java.io.InputStream.read(byte[])
or .read(byte[], int, int)
to read the exact number of bytes you need. (N.B. those methods both return an int
to indicate the number of bytes they actually read. You may need to call them in a loop to ensure that you fill your byte-array.) Don't worry about searching for \n
.
Upvotes: 1