Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #8490

Re: Buffered reading seems to corrupt data stream

From "Qu0ll" <Qu0llSixFour@gmail.com>
Newsgroups comp.lang.java.programmer
References <y4Cdne-uvN4kUBTTnZ2dnUVZ_hWdnZ2d@westnet.com.au>
Subject Re: Buffered reading seems to corrupt data stream
Date 2011-10-04 02:26 +1100
Message-ID <IfmdnSISO4_8SRTTnZ2dnUVZ_qmdnZ2d@westnet.com.au> (permalink)

Show all headers | View raw


"Qu0ll"  wrote in message 
news:y4Cdne-uvN4kUBTTnZ2dnUVZ_hWdnZ2d@westnet.com.au...

> 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?

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.

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
Qu0llSixFour@gmail.com
[Replace the "SixFour" with numbers to email me] 

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Buffered reading seems to corrupt data stream "Qu0ll" <Qu0llSixFour@gmail.com> - 2011-10-04 01:58 +1100
  Re: Buffered reading seems to corrupt data stream "Qu0ll" <Qu0llSixFour@gmail.com> - 2011-10-04 02:26 +1100
    Re: Buffered reading seems to corrupt data stream Ronny Schütz <usenet.r96@gishpuppy.com> - 2011-10-03 17:54 +0200
      Re: Buffered reading seems to corrupt data stream "Qu0ll" <Qu0llSixFour@gmail.com> - 2011-10-04 07:07 +1100
        Re: Buffered reading seems to corrupt data stream Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-03 13:18 -0700
      Re: Buffered reading seems to corrupt data stream "Qu0ll" <Qu0llSixFour@gmail.com> - 2011-10-04 10:54 +1100
    Re: Buffered reading seems to corrupt data stream Roedy Green <see_website@mindprod.com.invalid> - 2011-10-03 11:46 -0700
  Re: Buffered reading seems to corrupt data stream Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-10-03 08:44 -0700
  Re: Buffered reading seems to corrupt data stream markspace <-@.> - 2011-10-03 11:33 -0700
    Re: Buffered reading seems to corrupt data stream Lew <lewbloch@gmail.com> - 2011-10-04 08:48 -0700
      Re: Buffered reading seems to corrupt data stream Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-10-04 09:07 -0700
        Re: Buffered reading seems to corrupt data stream Lew <lewbloch@gmail.com> - 2011-10-04 12:06 -0700

csiph-web