jdk/src/share/classes/java/util/zip/CRC32.java
changeset 11113 f7248a4db8db
parent 5506 202f599c92aa
child 14342 8435a30053c1
--- a/jdk/src/share/classes/java/util/zip/CRC32.java	Tue Nov 29 11:39:59 2011 -0800
+++ b/jdk/src/share/classes/java/util/zip/CRC32.java	Tue Nov 29 13:05:35 2011 -0800
@@ -25,9 +25,15 @@
 
 package java.util.zip;
 
+import java.nio.ByteBuffer;
+import sun.nio.ch.DirectBuffer;
+
 /**
  * A class that can be used to compute the CRC-32 of a data stream.
  *
+ * <p> Passing a {@code null} argument to a method in this class will cause
+ * a {@link NullPointerException} to be thrown.
+ *
  * @see         Checksum
  * @author      David Connelly
  */
@@ -75,6 +81,38 @@
     }
 
     /**
+     * Updates the checksum with the bytes from the specified buffer.
+     *
+     * The checksum is updated using
+     * buffer.{@link java.nio.Buffer#remaining() remaining()}
+     * bytes starting at
+     * buffer.{@link java.nio.Buffer#position() position()}
+     * Upon return, the buffer's position will
+     * be updated to its limit; its limit will not have been changed.
+     *
+     * @param buffer the ByteBuffer to update the checksum with
+     * @since 1.8
+     */
+    public void update(ByteBuffer buffer) {
+        int pos = buffer.position();
+        int limit = buffer.limit();
+        assert (pos <= limit);
+        int rem = limit - pos;
+        if (rem <= 0)
+            return;
+        if (buffer instanceof DirectBuffer) {
+            crc = updateByteBuffer(crc, ((DirectBuffer)buffer).address(), pos, rem);
+        } else if (buffer.hasArray()) {
+            crc = updateBytes(crc, buffer.array(), pos + buffer.arrayOffset(), rem);
+        } else {
+            byte[] b = new byte[rem];
+            buffer.get(b);
+            crc = updateBytes(crc, b, 0, b.length);
+        }
+        buffer.position(limit);
+    }
+
+    /**
      * Resets CRC-32 to initial value.
      */
     public void reset() {
@@ -90,4 +128,7 @@
 
     private native static int update(int crc, int b);
     private native static int updateBytes(int crc, byte[] b, int off, int len);
+
+    private native static int updateByteBuffer(int adler, long addr,
+                                               int off, int len);
 }