Re: Buffered reading seems to corrupt data stream



The problem is that for the second or third read, the size variable is
coming back as garbage (extremely high or even negative value) and the
reading of the byte array blocks as it waits for a large amount of
data to be received or crashes with the invalid negative size even
though the previous size value and the previous bytes themselves are
correct. However, if I remove the BufferedInputStream wrapping of
stream, it works perfectly.

Why would that be?

I forgot to mention that there is only *one* input stream (is) and
output stream (os) being used in to the above code. Once the connection
between client and server is established I repeatedly send the fixed
byte array data (fixed for testing purposes) on the connected stream.

It might be, that the BufferedInputStream used for the first read
already read data belonging to the second message, that will get lost
when you dispose the BufferedInputStream and will be missing from the
next read call. The garbage size read is most likely actually a part of
the payload bytes. Try to synchronize server and client somehow to
verify. Better reuse the complete stream chain.

Ronny


.



Relevant Pages

  • Re: Writing binary output
    ... seems to insert garbage bytes into the binary file. ... Unformatted direct access can work, ... stream I/O to be added to f2003. ... most f95 compilers support it. ...
    (comp.lang.fortran)
  • Re: Why getInputStream in a http servlet request isnt getting the data sent by browser HTTP POST a
    ... BufferedInputStream is = new ... int count = is.read; ... That is only of interest if you are trying to avoid blocking, and not necessarily so much use even then. ... Furthermore, without looking at the source I speculate that BufferedInputStream's availablemethod might be based on the number of bytes available from the /buffer/, which might not be filled the first time until a read is attempted on the stream. ...
    (comp.lang.java.programmer)
  • Re: Civil War!
    ... stream of garbage? ... every hard working American to support your laziness and obseity? ... I hope you choke to death on your government ...
    (alt.sports.baseball.bos-redsox)
  • Re: Spawned process output
    ... BTW, the "consumer" does not consume the stream, but copies it to standard out. ... If you have an own buffer, there is no point in wrapping the stream in an extra BufferedInputStream. ... while (!isDone) ... Also, if you use this for the output from a child process, you are redirecting data from standard out and standard error from the child process to standard out only. ...
    (comp.lang.java.programmer)