vj said:
actually the protocol is not entirly lineoriented. The commands are
delimited by new line charectets but the data is handled as it is.
For instance there is a command AddImage 6481
This indicates that the client is going to send an image of size 6481
bytes. After receiving the command the next 6481 bytes that the client
sends will be not interpretted and are stored as it in a file.
As I think Oliver has already said, a protocol which mixes text and binary is
awkwardly defined. It isn't /too/ difficult to deal with the combination, but
it's more work than I guess you are expecting.
If you want to make it simple for yourself (or rather, make it as simple as the
text/binary combo ever gets), then treat the input as /binary/. You can
extract text data from a binary stream much more easily than you can extract
binary from text stream.
Read the input as binary, looking for sequences terminated by the binary
equivalent of \r\n (whatever that might be in the character encoding that the
sender is using -- almost certainly 0xD 0xA). Then convert that to text
(again, using whatever character encoding the sender is using -- you can
probably assume US-ASCII), and decode the interpret the resulting string in the
way you already are doing. When a command announces that 6481 of binary data
will be the next thing to arrive, you don't need to do anything special, just
consume the next 6481 bytes of input.
If you don't do that, then the binary->text conversion (in the java.io.Reader)
will have converted the binary data into text, and the best that can happen
then is that you have to work out how to reverse that transformation. You
/might/ be able to do that (depending on the encoding, it can be fairly easy or
totally impossible) but it'll probably be difficult and will almost certainly
be inefficient.
-- chris