Problem with BufferedInputStream

Steve Ratcliffe sterat at dial.pipex.com
Mon Sep 7 16:38:03 PDT 1998


There is a problem with the one argument read method of
BufferedInputStream.  It goes directly to the underlying file rather
than reading out of the buffer. For example:

	FileInputStream fis = new FileInputStream("foo");
	BufferedInputStream is = new BufferedInputStream(fis);
	c = is.read();  // Fill the buffer and read first byte.
	is.read(b);	// Wrong bytes read here; gets correct bytes
				// without preceeding line

The real problem is in the underlying FilterInputStream method I think.
The one arg method should call the three arg method.  (This change
then requires that the 3 arg method be used in the BufferedInputStream
constructor)

A possible fix follows.

..Steve

--- BufferedInputStream.java.orig	Mon Sep  7 23:28:00 1998
+++ BufferedInputStream.java	Mon Sep  7 23:28:19 1998
@@ -41,7 +41,7 @@
 	marklimit = 0;
 	pos = 0;
 	try {
-		count = super.read(buf);
+		count = super.read(buf, 0, buf.length);
 	} catch (IOException e) {
 		count = -1;
 	}
--- FilterInputStream.java.orig	Mon Sep  7 23:19:32 1998
+++ FilterInputStream.java	Mon Sep  7 23:27:24 1998
@@ -71,7 +71,7 @@
 public int read(byte b[]) throws IOException
 {
 	try {
-		return (in.read(b));
+		return (read(b, 0, b.length));
 	}
 	catch (NullPointerException _) {
 		throw new EOFException("null stream");


More information about the kaffe mailing list