[kaffe] DataInputStream related merge

Jukka Santala jsantala at morphine.tml.hut.fi
Fri Oct 4 07:14:40 PDT 2002


Fairly straightforward merge from PocketLinux version, only problem is
neither version of Kaffe has exact implementation. With Kaffe.org's
version the problem is actually encountered, though - using Xerces XML
parser, the HTTP code will eat out the headers using readLine, but then
Xerces will use something else than DataInputStream to parse the remaining
data. Because that reader doesn't know to eat the extra line-feed, it gets
stuck at the beginning of the parse-input, which then doesn't fullfill the
XML-production condition, and causes all XML documents read over HTTP to
fail. If anybody can come up with an implementation that doesn't need
mark()/reset(), I'd be happy. If not, this corrects few exception issues
and at least works, unlike the old code :)

 -Jukka Santala
-------------- next part --------------
Index: libraries/javalib/java/io/DataInput.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/java/io/DataInput.java,v
retrieving revision 1.5
diff -u -r1.5 DataInput.java
--- libraries/javalib/java/io/DataInput.java	24 Nov 2001 01:52:49 -0000	1.5
+++ libraries/javalib/java/io/DataInput.java	4 Oct 2002 14:02:50 -0000
@@ -11,23 +11,24 @@
  * of this file.
  */
 
+import java.lang.String;
 
 public interface DataInput {
 
-boolean readBoolean() throws IOException;
-byte readByte() throws IOException;
-char readChar() throws IOException;
-double readDouble() throws IOException;
-float readFloat() throws IOException;
-void readFully(byte b[]) throws IOException;
-void readFully(byte b[], int off, int len) throws IOException;
-int readInt() throws IOException;
-String readLine() throws IOException;
-long readLong() throws IOException;
-short readShort() throws IOException;
-String readUTF() throws IOException;
-int readUnsignedByte() throws IOException;
-int readUnsignedShort() throws IOException;
-int skipBytes(int n) throws IOException;
+abstract public boolean readBoolean() throws IOException;
+abstract public byte readByte() throws IOException;
+abstract public char readChar() throws IOException;
+abstract public double readDouble() throws IOException;
+abstract public float readFloat() throws IOException;
+abstract public void readFully(byte b[]) throws IOException;
+abstract public void readFully(byte b[], int off, int len) throws IOException;
+abstract public int readInt() throws IOException;
+abstract public String readLine() throws IOException;
+abstract public long readLong() throws IOException;
+abstract public short readShort() throws IOException;
+abstract public String readUTF() throws IOException;
+abstract public int readUnsignedByte() throws IOException;
+abstract public int readUnsignedShort() throws IOException;
+abstract public int skipBytes(int n) throws IOException;
 
 }
Index: libraries/javalib/java/io/DataInputStream.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/java/io/DataInputStream.java,v
retrieving revision 1.15
diff -u -r1.15 DataInputStream.java
--- libraries/javalib/java/io/DataInputStream.java	3 Sep 2002 19:53:47 -0000	1.15
+++ libraries/javalib/java/io/DataInputStream.java	4 Oct 2002 14:02:50 -0000
@@ -13,8 +13,6 @@
 import kaffe.util.UTF8;
 
 public class DataInputStream extends FilterInputStream implements DataInput {
-	/* used to determine whether to skip next '\n' */
-	private boolean skipNextLF;
 
 public DataInputStream(InputStream in) {
 	super(in);
@@ -25,10 +23,6 @@
 }
 
 public final int read(byte b[], int off, int len) throws IOException {
-	if (off < 0 || len < 0 || off + len > b.length) {
-	   throw new IndexOutOfBoundsException();
-	}
-
 	return super.read(b, off, len);
 }
 
@@ -41,18 +35,15 @@
 	if (value == -1) {
 		throw new EOFException();
 	}
-	if (skipNextLF) {
-		skipNextLF = false;
-		if (value == '\n') {
-			value = readByte();
-		}
-	}
 	return ((byte)value);
 }
 
 public final char readChar() throws IOException {
-	int val = readUnsignedByte() << 8;
-	val |= readUnsignedByte();
+	int val = read() << 8;
+	val |= read();
+	if (val == -1) {
+		throw new EOFException();
+	}
 	return ((char)val);
 }
 
@@ -69,9 +60,12 @@
 }
 
 public final void readFully(byte b[], int off, int len) throws IOException {
+	if (b == null) {
+		throw new NullPointerException();
+	}
 	int total = 0;
 	while (total < len) {
-		final int got = read(b, off + total, len - total);
+		int got = read(b, off + total, len - total);
 		if (got == -1) {
 			throw new EOFException();
 		}
@@ -80,11 +74,14 @@
 }
 
 public final int readInt() throws IOException {
-	int v1 = readUnsignedByte() << 24;
-	v1 |= readUnsignedByte() << 16;
-	v1 |= readUnsignedByte() << 8;
-	v1 |= readUnsignedByte();
-	return v1;
+	int v1 = read() << 24;
+	v1 |= read() << 16;
+	v1 |= read() << 8;
+	int v2 = read();
+	if (v2 == -1) {
+		throw new EOFException();
+	}
+	return (v1 | v2);
 }
 
 /**
@@ -92,27 +89,43 @@
  */
 public final String readLine() throws IOException {
 	final StringBuffer buffer = new StringBuffer();
+	final byte[] data = new byte[1];
 	boolean eof = false;
 
-	try {
-		while (true) {
-			final char ch = (char) readUnsignedByte();
-			if (ch == '\n') {
-				break;
-			}
-			if (ch == '\r') {
-				skipNextLF = true;
-				break;
-			}
+	while (true) {
+		if (read(data, 0, 1) != 1) {
+			eof = true;
+			break;
+		}
+		final char ch = (char) (data[0] & 0xff);
+		if (ch == '\n') {
+			break;
+		}
 
-			buffer.append(ch);
+		if (ch == '\r') {       // Check for '\r\n'
+			// Note that we don't know whether the InputStream
+			// implements mark() and reset(), but we're using
+			// them anyway. If they don't, then characters
+			// after a lone '\r' will be elided from the input
+			// (ie, this is a bug). We could override mark()
+			// and reset() to always provide at least a
+			// one-character buffer, but then we'd violate
+			// the spec, which says to inherit these from
+			// FilterInputStream...
+
+			super.mark(1);
+			if (read(data, 0, 1) == 1 && data[0] != '\n') {
+				try {
+					super.reset();
+				}
+				catch (IOException e) {
+				}
+			}
+			break;
 		}
+		buffer.append(ch);
 	}
-	catch (EOFException e) {
-		eof = true;
-	}
-
-	if (eof && buffer.length() == 0) {
+	if (eof == true && buffer.length() == 0) {
 		return (null);
 	}
 	return (buffer.toString());
@@ -123,8 +136,11 @@
 }
 
 public final short readShort() throws IOException {
-	int val = readUnsignedByte() << 8;
-	val |= readUnsignedByte();
+	int val = read() << 8;
+	val |= read();
+	if (val == -1) {
+		throw new EOFException();
+	}
 	return ((short)val);
 }
 


More information about the kaffe mailing list