Bug in java.io.RandomAccessFile.readLine()

Marc Jauvin mjauvin at videotron.ca
Mon Oct 19 21:56:25 PDT 1998


Hello all, 

I discovered a bug in the readLine() method in 'java.io.RandomAccessFile'; the
problem was caused by trying to cast an int value into a byte. It might not have
caused problem on some architectures, but it did on x86... one other thing, the
'\r' and '\n' were returned as part of the string, which is not the behavior of
the implementation in java.io.BufferedReader.readLine()  (neither it is in SUN's
jdk if I remember correctly by USING their readLine() functions)

So here is a patch file! (created with diff -urN file.orig file.mod)

[extract with "cd /usr/local/kaffe; patch -p1 < RandomAccessFile.diff"]

-- mj
-------------- next part --------------
--- kaffe-1.0.b2/libraries/javalib/java/io/RandomAccessFile.java	Tue Oct 20 00:22:18 1998
+++ kaffe-1.0.b2/libraries/javalib/java/io/RandomAccessFile.java-mj	Tue Oct 20 00:22:11 1998
@@ -116,36 +116,37 @@
 }
 
 final public String readLine() throws IOException {
+	int data=-1;
 	boolean EOL=false;
 	long ptr;
 	StringBuffer buffer=new StringBuffer();
 
 	while (!EOL) {
-		int data=read();
+		data=read();
 
 		if (data==-1) {
 			EOL=true;
 		}
 		else {
-			buffer.append((byte)data);
 			if ((char )data=='\n') EOL=true;
-			if ((char )data=='\r') {
+			else if ((char )data=='\r') {
 				/* Check for "\r\n" */
+				EOL=true;
 				ptr=getFilePointer();
 				int nextByte=read();
 
-				if (nextByte==-1) EOL=true;
-				else if ((char )nextByte=='\n') {
-					buffer.append((byte)nextByte);
-					EOL=true;
-				}
-				else {
+				if ((char )nextByte!='\n' && nextByte!=-1) {
 					/* Jump back to ptr */
 					seek(ptr);
 				}
 			}
+			else buffer.append((char)data);
 		}
 	}
+
+        if ((data == -1) && (buffer.length() == 0)) {
+            return null;
+        }
 
 	return buffer.toString();
 }


More information about the kaffe mailing list