Buffered reading seems to corrupt data stream
- From: "Qu0ll" <Qu0llSixFour@xxxxxxxxx>
- Date: Tue, 4 Oct 2011 01:58:41 +1100
I am trying to repeatedly send a byte array from a server to a client where both the writing and the reading is done in a buffered manner with the following code on the server:
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(bytes.length);
dos.write(bytes);
dos.flush();
and this on the client:
System.out.println("Reading size...");
final DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
final int size = dis.readInt();
final byte[] bytes = new byte[size];
System.out.println("Reading " + size + " bytes...");
dis.readFully(bytes);
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?
--
And loving it,
-Qu0ll (Rare, not extinct)
_________________________________________________
Qu0llSixFour@xxxxxxxxx
[Replace the "SixFour" with numbers to email me]
.
- Follow-Ups:
- Re: Buffered reading seems to corrupt data stream
- From: Peter Duniho
- Re: Buffered reading seems to corrupt data stream
- From: Qu0ll
- Re: Buffered reading seems to corrupt data stream
- Prev by Date: Re: Avoid creating a stacktrace prior to JDK 1.7
- Next by Date: Re: Buffered reading seems to corrupt data stream
- Previous by thread: Re: Thumbs up for suppressable exceptions in JDK 1.7
- Next by thread: Re: Buffered reading seems to corrupt data stream
- Index(es):
Relevant Pages
|