ldiqual
ldiqual

Reputation: 15365

Separate ASCII from binary in a socket transmission

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%]

I'm facing two problems:

Upvotes: 1

Views: 201

Answers (1)

ruakh
ruakh

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

Related Questions