[587] in java-interest

home help back first fref pref prev next nref lref last post

Bug in PipedInputStream

daemon@ATHENA.MIT.EDU (Robert Lougher)
Tue Jul 4 14:15:48 1995

From: "Robert Lougher" <rob@comp.lancs.ac.uk>
Date: Tue, 4 Jul 1995 15:29:15 +0100
To: java-interest@java.Eng.Sun.COM

I found a bug in PipedInputStream while playing around with communication
between threads last night.  The method read() should return the byte
written or -1 if the stream has ended.  However, writing -1 also results in
an integer value of -1.  This means it is impossible to tell whether the byte
was -1 or whether the stream has ended.

The int type implies it should work in the same way as streams in ANSI C (i.e.
fgetc, fputc, etc.), and FileInputStream does indeed work this way.  Looking
at the code for PipedInputStream it appears the programmer forgot about this
and the (signed) bytes in the buffer are simply cast to int.  The easiest
way to correct this is to convert the int to its unsigned value if it is less
than zero.

To do this, change line 116 of ?/hotjava/classsrc/java/io/PipedInputStream.java
from 'return ret;' to 'return ret < 0 ? 256 + ret : ret;'.  Recompile and
move the new .class file to the ?/hotjava/classes/java/io directory.

Robert Lougher

---------------------------------------------------------------------
POST:  Dr. Robert Lougher            | email: rob@comp.lancs.ac.uk
       Department of Computing       | tel. : +44 1524 65201 x3808
       University of Lancaster       | fax. : +44 1524 593608
       Lancaster, LA1 4YR, UK.       |

-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post