Hi,

I tried to get my java bittorrent project (http://www.klomp.org/snark/),
working with kaffe and found a problem with BufferedInputStream. The
code contains the following construct (simplified, the real code uses a
Socket InputStream):

public class ReadLineTest
{
  public static void main(String args[]) throws Exception
  {
    InputStream in = System.in;
    BufferedInputStream bis = new BufferedInputStream(in);
    BufferedReader br = new BufferedReader
      (new InputStreamReader(bis, "US-ASCII"));
    String line = br.readLine();
    System.out.println(line);
  }
}

The expected behaviour is for the program to read a line and print it.
But readLine() will not return with the first line till at least 2048
characters have been input (which can be multiple lines).

The problem is in BufferedInputStream._read(byte b[], int off, int len).
Which tries to fill the given byte[] with len bytes even if it has
buffered less bytes (so it will keep readingfrom the underlying
InputStream).

The attached patch removes the while (len > 0) construct which makes the
above program (and The Hunting of the Snark Project) work correctly.

Mauve and make check results do not show regressions with this change.

Cheers,

Mark
Index: libraries/javalib/java/io/BufferedInputStream.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/java/io/BufferedInputStream.java,v
retrieving revision 1.10
diff -u -r1.10 BufferedInputStream.java
--- libraries/javalib/java/io/BufferedInputStream.java	4 Apr 2003 06:11:41 -0000	1.10
+++ libraries/javalib/java/io/BufferedInputStream.java	5 Jun 2003 12:22:38 -0000
@@ -102,44 +102,34 @@
 		return (1);
 	}
 
-	int total = 0;
-	while (len > 0) {
-
-		// If buffer fully consumed, invalidate mark & reset buffer
-		if (pos == buf.length) {
-			pos = count = 0;
-			markpos = -1;
-		}
-
-		// Buffer empty?
-		int nread;
-		if (pos == count) {
+	// If buffer fully consumed, invalidate mark & reset buffer
+	if (pos == buf.length) {
+		pos = count = 0;
+		markpos = -1;
+	}
 
-			// If the amount requested is more than the size
-			// of the buffer, we might as well optimize with
-			// a direct read to avoid needless copying of data.
-			if (len >= buf.length) {
-				if ((nread = super.read(b, off, len)) == -1) {
-					return (total > 0) ? total : -1;
-				}
-				return total + nread;
-			}
+	// Buffer empty?
+	if (pos == count) {
 
-			// Read another buffer's worth of data
-			if (!fillBuffer()) {
-				return (total > 0) ? total : -1;
-			}
+		// If the amount requested is more than the size
+		// of the buffer, we might as well optimize with
+		// a direct read to avoid needless copying of data.
+		if (len >= buf.length) {
+			return super.read(b, off, len);
 		}
 
-		// Copy the next chunk of bytes from our buffer
-		nread = Math.min(count - pos, len);
-		System.arraycopy(buf, pos, b, off, nread);
-		total += nread;
-		pos += nread;
-		off += nread;
-		len -= nread;
+		// Read another buffer's worth of data
+		if (!fillBuffer()) {
+			return -1;
+		}
 	}
-	return total;
+
+	// Copy the next chunk of bytes from our buffer
+	int nread = Math.min(count - pos, len);
+	System.arraycopy(buf, pos, b, off, nread);
+	pos += nread;
+
+	return nread;
 }
 
 public synchronized void reset() throws IOException {

Reply via email to