Re: Buffered reading seems to corrupt data stream



On 10/3/11 7:58 AM, Qu0ll wrote:
[...]
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?

If you don't post a concise-but-complete code example that reliably reproduces the problem (i.e. an SSCCE), there's no way to answer that question.

The problem you are describing is typical of failure to treat the streams as streams, but of course that's the kind of thing that DataInputStream is supposed to address for you (and which I believe will, as long as you use it correctly…unless perhaps you're using some odd-ball Java implementation that might be more buggy than the ones you'd usually find on mainstream OSs).

Another way I suppose you could observe the behavior you're describing is to be using a datagram socket instead of a stream-oriented socket.

But without a proper code example, there's no way to know what you've done wrong.

Pete
.



Relevant Pages

  • Re: Elisabeth Hasselbeck says Carrie Prejean is fighting for her life
    ... please proceed to the nearest dump if you're going to allow ... streams of garbage to come out of your mouth. ...
    (rec.arts.tv)
  • Re: TCP Stream
    ... There are exactly two reasons for you to receive garbage ... from a TCP socket: ... Microsoft MVP, MCSD ... streams have no boundaries is there a way to detect where the original ...
    (microsoft.public.win32.programmer.networks)