Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!69.16.185.11.MISMATCH!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!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 09:59:05 -0500 From: "Qu0ll" Newsgroups: comp.lang.java.programmer Subject: Buffered reading seems to corrupt data stream Date: Tue, 4 Oct 2011 01:58:41 +1100 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original 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: 36 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 124.168.28.143 X-Trace: sv3-K8h8ellJvmKK7wXd03A4vBNnEP9NfauLotmcfojr8QeZTyoC5Z5yxjVD2x3EwdNKQ7rVwVMYg/PYhh1!7kYMuHUr0T1Kx5ucwrhQfER1bJuL6joY282+7dg2jkzvfiLa5PatajCjD9qErXkSZVx2qyCDnabL!5pDN8G9NIruiwA9sy9Kiig== 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: 2457 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8489 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@gmail.com [Replace the "SixFour" with numbers to email me]