Index: java/util/zip/Adler32.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/util/zip/Adler32.java,v retrieving revision 1.5 diff -u -r1.5 Adler32.java --- java/util/zip/Adler32.java 22 Jan 2002 22:27:02 -0000 1.5 +++ java/util/zip/Adler32.java 15 May 2005 19:46:45 -0000 @@ -1,5 +1,5 @@ /* Adler32.java - Computes Adler32 data checksum of a data stream - Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. + Copyright (C) 1999, 2000, 2001, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -153,6 +153,19 @@ } /** + * In update() we can defer the modulo operation: + * s1 maximally grows from 65521 (BASE) to 65521 + 255 * 3800 + * s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31 + * Calculated this value like this: + * + * int i = 0, a = BASE, b = a; + * for(; b > 0; i++) + * b += (a += 255); + * System.out.println("max defer = " + (i - 1)); + */ + private static final int DEFER = 3850; + + /** * Updates the checksum with the bytes taken from the array. * * @param buf an array of bytes @@ -161,20 +174,13 @@ */ public void update (byte[] buf, int off, int len) { - //(By Per Bothner) int s1 = checksum & 0xffff; int s2 = checksum >>> 16; - - while (len > 0) + + while (off < len) { - // We can defer the modulo operation: - // s1 maximally grows from 65521 to 65521 + 255 * 3800 - // s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31 - int n = 3800; - if (n > len) - n = len; - len -= n; - while (--n >= 0) + int max = Math.min(off + DEFER, len); + while (off < max) { s1 = s1 + (buf[off++] & 0xFF); s2 = s2 + s1; @@ -182,16 +188,6 @@ s1 %= BASE; s2 %= BASE; } - - /*Old implementation, borrowed from somewhere: - int n; - - while (len-- > 0) { - - s1 = (s1 + (bs[offset++] & 0xff)) % BASE; - s2 = (s2 + s1) % BASE; - }*/ - checksum = (s2 << 16) | s1; }