Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File
```*** old/src/share/classes/java/util/zip/Adler32.java	Tue May 21 16:49:14 2013
--- new/src/share/classes/java/util/zip/Adler32.java	Tue May 21 16:49:14 2013

*** 1,7 ****
--- 1,7 ----
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as

*** 39,50 ****
--- 39,80 ----
* @see         Checksum
* @author      David Connelly
*/
public
+     /*
+      * The Adler calculation is regarded as
+      * taking an input text T of length N = |T|,
+      * and computing two quantities, A(T) and B(T),
+      * where A(T) = 1 + sum_{0 <= i < |T|}(T_i)
+      * and B(T) = |T| + sum_{0 <= i < |T|}((|T| - i) * T_i),
+      * both modulo 65521.
+      *
+      * However, with sufficient algebraic manipulation, one can derive
+      * that A(U||V) = A(U) + A(V) - 1
+      * and B(U||V) = B(U) + B(V) + |V| (A(U) - 1).
+      *
+      * This allows recursive subdivision, if that is considered desirable.
+      */
+
+     /**
+      * The modulo operation can be deferred for MAX_SLOP bytes of input, permitting
+      * faster byte-by-byte Adler computations.  1024 is plenty conservative.
+      */
+     private final static int MAX_SLOP = 1024;
+
+     /**
+      * For inputs smaller than JAVA_ADLER_BELOW JNI overheads make it faster
+      * to compute on the Java side.  (This may change as overheads and compiler
+      * quality change).
+      */
+     private final static int JAVA_ADLER_BELOW = 32;

+     private int aa = 1;
+     private int bb = 0;
+     private int slop = 0;

/**
* Creates a new Adler32 object.
*/

*** 55,65 ****
--- 85,101 ----
* bits of the argument b).
*
* @param b the byte to update the checksum with
*/
public void update(int b) {
!         int la = aa + (b & 0xFF);
+         bb = bb + la;
+         aa = la;
+         slop++;
+         if (slop == MAX_SLOP) {
+             getValueI();
+         }
}

/**
* Updates the checksum with the specified array of bytes.
*/

*** 68,89 ****
--- 104,134 ----
throw new NullPointerException();
}
if (off < 0 || len < 0 || off > b.length - len) {
throw new ArrayIndexOutOfBoundsException();
}
+         if (len < JAVA_ADLER_BELOW) {
+             for (int i = 0; i < len; i++)
+                 update(b[i+off]);
+         } else {
+             setValue(updateBytes(getValueI(), b, off, len));
+         }
}

/**
* Updates the checksum with the specified array of bytes.
*
* @param b the byte array to update the checksum with
*/
public void update(byte[] b) {
+         if (b.length < JAVA_ADLER_BELOW) {
+             for (int i = 0; i < b.length; i++)
+                 update(b[i]);
+         } else {
+             setValue(updateBytes(getValueI(), b, 0, b.length));
+         }
}

/**
* Updates the checksum with the bytes from the specified buffer.
*
* The checksum is updated using

*** 102,134 ****
--- 147,199 ----
assert (pos <= limit);
int rem = limit - pos;
if (rem <= 0)
return;
if (buffer instanceof DirectBuffer) {
} else if (buffer.hasArray()) {
!             setValue(updateBytes(getValueI(), buffer.array(), pos + buffer.arrayOffset(), rem));
} else {
byte[] b = new byte[rem];
buffer.get(b);
!             setValue(updateBytes(getValueI(), b, 0, b.length));
}
buffer.position(limit);
}

/**
* Resets the checksum to initial value.
*/
public void reset() {
+         aa = 1;
+         bb = 0;
+         slop = 0;
}

/**
* Returns the checksum value.
*/
public long getValue() {
!         return getValueI() & 0xffffffffL;
+     }
+
+     private int getValueI() {
+         if (slop > 0) {
+             aa = aa % 65521;
+             bb = bb % 65521;
+             adler = (bb << 16) + aa;
+             slop = 0;
+         }
+     }
+
+     private void setValue(int newValue) {
+         aa = newValue & 0xffff;
+         bb = newValue >>> 16;