Two patches

Oskar Sandberg kaffe@rufus.w3.org
Wed, 29 Aug 2001 12:25:20 +0200



--pWyiEgJYm5f9v55/
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

I have attached a small diff for two issues I have run into with Kaffe
lately.

The first is a problem with BufferedReader, where fillOutBuffer() will
reset the buffer position before reading, so if an exception is thrown
during fillOutBuffer the next read will end up returning whatever was in
the buffer previously (this pretty much only comes up when using an
SoTimeout since most IOExceptions are terminal). It's a common bug I
think, Microsoft's JREs seem to all the problem - though Kaffe's
BufferedInputStream does not. 

The second issue is that BigInteger doesn't return consistant hashcode
values, which it does with all other JREs I have run into. The
hashCode() added divides and extracts ints, XORing them together. Since
this is a little slow I made it cache the result. 

-- 
'DeCSS would be fine. Where is it?'
'Here,' Montag touched his head.
'Ah,' Granger smiled and nodded.

Oskar Sandberg
oskar@freenetproject.org

--pWyiEgJYm5f9v55/
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="BRBI.diff"

Index: libraries/javalib/java/io/BufferedReader.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/java/io/BufferedReader.java,v
retrieving revision 1.9
diff -u -r1.9 BufferedReader.java
--- libraries/javalib/java/io/BufferedReader.java	2000/07/14 23:25:21	1.9
+++ libraries/javalib/java/io/BufferedReader.java	2001/08/29 10:05:12
@@ -222,27 +222,31 @@
 
 private int fillOutBuffer () throws IOException {
 	synchronized ( lock ) {
-		int n;
+            int n;
+            int newpos;
+            
+            if (markset) {
+                if (pos == inbuf.length) {	// mark overflow
+                    markvalid = false;
+                    newpos = 0;
+                } else {
+                    newpos = pos;
+                } 
+            } else {
+                newpos = 0;
+            }
 
-		if (markset) {
-			if (pos == inbuf.length) {	// mark overflow
-				markvalid = false;
-				pos = 0;
-			}
-		} else {
-			pos = 0;
-		}
+            n = rd.read(inbuf, newpos, inbuf.length - newpos);
 
-		n = rd.read(inbuf, pos, inbuf.length - pos);
-
-		if (n >= 0) {
-			size = pos + n;
-			return (n);
-		}
-		else {
-			size = pos;
-			return (-1);
-		}
+            if (n >= 0) {
+                pos = newpos;
+                size = pos + n;
+                return (n);
+            } else {
+                pos = newpos;
+                size = pos;
+                return (-1);
+            }
 	}
 }
 
Index: libraries/javalib/java/math/BigInteger.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/java/math/BigInteger.java,v
retrieving revision 1.18
diff -u -r1.18 BigInteger.java
--- libraries/javalib/java/math/BigInteger.java	2000/12/26 13:57:20	1.18
+++ libraries/javalib/java/math/BigInteger.java	2001/08/29 10:05:13
@@ -21,6 +21,7 @@
 
 private static final long serialVersionUID = -8287574255936472291L;
 private Ptr number;
+    private Integer hcode;
 
 public static final BigInteger ZERO;
 public static final BigInteger ONE;
@@ -365,8 +366,21 @@
 }
 
 public int hashCode() {
-	// It probably isn't this but I don't know what it's suppose to be.
-	return (super.hashCode());
+    if (hcode == null) {
+        int sign = cmp0(this, ZERO);
+        BigInteger copy = abs(), divisor = new BigInteger();
+        divisor.setbit0(divisor, 32); // prepare to shift right
+        int code = 0;
+        for (int i = bitLength() / 8 ; i > 4 ; i -= 4) {
+            code ^= copy.toInt0();
+            copy.div0(copy, divisor);
+        }
+        code ^= copy.toInt0();
+        code *= sign;
+        hcode = new Integer(code);
+        return code;
+    }
+    return hcode.intValue();
 }
 
 public String toString(int radix) {

--pWyiEgJYm5f9v55/--