On Mar 30, 11:15 am, "Chris Uppal" <chris.up...@metagnostic.REMOVE-
THIS.org> wrote:
> > If you are in control of the protocol, then you should redesign it, as
> > you are trying to apply a "line oriented protocol" (where the concept of a
[quoted text clipped - 7 lines]
>
> -- chris
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.
The problem is occuring due to the way i am handing this scenario.
Presently i use a stringbuffer to buffer all this data. partly because
finding the length of stored data in ByteArrayOutputStream class is
inefficient ( you need to call toByteArray().length, that will create
a new new copy!!).
In stringbuffer you simply need to call .length() and we have the
length. And i think thats where the problem lies.
Redesigning the protocol is an option but i would like to avoid it as
much as possible.
vj - 30 Mar 2007 11:27 GMT
I found yet another thread
http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/b3
82e6a714a73f90
in this group that is describing problem similar to me. Here you guys
have said that when storing binary data in a string/stringbuffer you
need to be sure abt the charecter-set that has being used.
Piotr Kobzda - 30 Mar 2007 13:16 GMT
> The problem is occuring due to the way i am handing this scenario.
> Presently i use a stringbuffer to buffer all this data. partly because
[quoted text clipped - 3 lines]
> In stringbuffer you simply need to call .length() and we have the
> length. And i think thats where the problem lies.
Thus familiarize yourself with size() method from ByteArrayOutputStream.
It should solve the problem.
piotr
Chris Uppal - 30 Mar 2007 14:56 GMT
> actually the protocol is not entirly lineoriented. The commands are
> delimited by new line charectets but the data is handled as it is.
[quoted text clipped - 3 lines]
> 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