Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.westnet.com.au!news.westnet.com.au.POSTED!not-for-mail NNTP-Posting-Date: Mon, 03 Oct 2011 10:27:28 -0500 From: "Qu0ll" Newsgroups: comp.lang.java.programmer References: In-Reply-To: Subject: Re: Buffered reading seems to corrupt data stream Date: Tue, 4 Oct 2011 02:26:45 +1100 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal Importance: Normal X-Newsreader: Microsoft Windows Live Mail 15.4.3538.513 X-MimeOLE: Produced By Microsoft MimeOLE V15.4.3538.513 Message-ID: Lines: 45 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 124.168.28.143 X-Trace: sv3-9O7iznm3cKEdF+m7iCJGwr2vpSSr1R913G/PQn4dC57Quw/Fe0gk61y5NYx/1u21zkTh5mYGkyRLxD1!vFE27VynWNIwfOz2+pgqDjvFBZFDOOpU2zX16SASgieiMH98+fYIxAD241FFIrkhx4LPuH/oUjG+!nM/wJNhMx9CEs/AODNxLMw== X-Complaints-To: abuse@westnet.com.au X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3014 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8490 "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]