changeset 4544:c42b880f2f4b

7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer Summary: Added ByteBuffer support via com.oracle.util.Checksums Reviewed-by: alanb
author sherman
date Fri, 16 Dec 2011 14:27:41 -0800
parents c103b64d681b
children 8ee4164ea612
files make/common/Release.gmk make/java/zip/mapfile-vers src/share/classes/java/util/zip/Adler32.java src/share/classes/sun/misc/JavaUtilZipAccess.java src/share/classes/sun/misc/SharedSecrets.java src/share/native/java/util/zip/Adler32.c
diffstat 6 files changed, 105 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/make/common/Release.gmk	Thu Dec 15 14:19:32 2011 +0400
+++ b/make/common/Release.gmk	Fri Dec 16 14:27:41 2011 -0800
@@ -61,7 +61,8 @@
 #
 EXPORTED_PRIVATE_PKGS = com.sun.servicetag \
                         com.oracle.net \
-                        com.oracle.nio
+                        com.oracle.nio \
+                        com.oracle.util
 
 # 64-bit solaris has a few special cases. We define the variable
 # SOLARIS64 for use in this Makefile to easily test those cases
--- a/make/java/zip/mapfile-vers	Thu Dec 15 14:19:32 2011 +0400
+++ b/make/java/zip/mapfile-vers	Fri Dec 16 14:27:41 2011 -0800
@@ -30,6 +30,7 @@
 		Java_java_util_jar_JarFile_getMetaInfEntryNames;
 		Java_java_util_zip_Adler32_update;
 		Java_java_util_zip_Adler32_updateBytes;
+		Java_java_util_zip_Adler32_updateByteBuffer;
 		Java_java_util_zip_CRC32_update;
 		Java_java_util_zip_CRC32_updateBytes;
 		Java_java_util_zip_Deflater_deflateBytes;
--- a/src/share/classes/java/util/zip/Adler32.java	Thu Dec 15 14:19:32 2011 +0400
+++ b/src/share/classes/java/util/zip/Adler32.java	Fri Dec 16 14:27:41 2011 -0800
@@ -25,6 +25,9 @@
 
 package java.util.zip;
 
+import java.nio.ByteBuffer;
+import sun.nio.ch.DirectBuffer;
+
 /**
  * A class that can be used to compute the Adler-32 checksum of a data
  * stream. An Adler-32 checksum is almost as reliable as a CRC-32 but
@@ -76,6 +79,37 @@
     }
 
     /**
+     * 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
+     */
+    private 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) {
+            adler = updateByteBuffer(adler, ((DirectBuffer)buffer).address(), pos, rem);
+        } else if (buffer.hasArray()) {
+            adler = updateBytes(adler, buffer.array(), pos + buffer.arrayOffset(), rem);
+        } else {
+            byte[] b = new byte[rem];
+            buffer.get(b);
+            adler = updateBytes(adler, b, 0, b.length);
+        }
+        buffer.position(limit);
+    }
+
+    /**
      * Resets the checksum to initial value.
      */
     public void reset() {
@@ -89,7 +123,18 @@
         return (long)adler & 0xffffffffL;
     }
 
+    // Set up JavaUtilZipAccess in SharedSecrets
+    static {
+       sun.misc.SharedSecrets.setJavaUtilZipAccess(new sun.misc.JavaUtilZipAccess() {
+           public void update(Adler32 adler32, ByteBuffer buf) {
+               adler32.update(buf);
+           }
+        });
+    }
+
     private native static int update(int adler, int b);
     private native static int updateBytes(int adler, byte[] b, int off,
                                           int len);
+    private native static int updateByteBuffer(int adler, long addr,
+                                               int off, int len);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/sun/misc/JavaUtilZipAccess.java	Fri Dec 16 14:27:41 2011 -0800
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * 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
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package sun.misc;
+
+import java.util.zip.Adler32;
+import java.nio.ByteBuffer;
+
+public interface JavaUtilZipAccess {
+    public void update(Adler32 adler32, ByteBuffer buf);
+}
--- a/src/share/classes/sun/misc/SharedSecrets.java	Thu Dec 15 14:19:32 2011 +0400
+++ b/src/share/classes/sun/misc/SharedSecrets.java	Fri Dec 16 14:27:41 2011 -0800
@@ -29,6 +29,7 @@
 import java.io.Console;
 import java.io.FileDescriptor;
 import java.security.ProtectionDomain;
+import java.util.zip.Adler32;
 import javax.security.auth.kerberos.KeyTab;
 
 import java.security.AccessController;
@@ -53,6 +54,7 @@
     private static JavaSecurityProtectionDomainAccess javaSecurityProtectionDomainAccess;
     private static JavaSecurityAccess javaSecurityAccess;
     private static JavaxSecurityAuthKerberosAccess javaxSecurityAuthKerberosAccess;
+    private static JavaUtilZipAccess javaUtilZipAccess;
 
     public static JavaUtilJarAccess javaUtilJarAccess() {
         if (javaUtilJarAccess == null) {
@@ -153,4 +155,15 @@
             unsafe.ensureClassInitialized(KeyTab.class);
         return javaxSecurityAuthKerberosAccess;
     }
+
+    public static void setJavaUtilZipAccess(JavaUtilZipAccess access) {
+        javaUtilZipAccess = access;
+    }
+
+    public static JavaUtilZipAccess getJavaUtilZipAccess() {
+        if (javaUtilZipAccess == null) {
+            unsafe.ensureClassInitialized(Adler32.class);
+        }
+        return javaUtilZipAccess;
+    }
 }
--- a/src/share/native/java/util/zip/Adler32.c	Thu Dec 15 14:19:32 2011 +0400
+++ b/src/share/native/java/util/zip/Adler32.c	Fri Dec 16 14:27:41 2011 -0800
@@ -53,3 +53,14 @@
     }
     return adler;
 }
+
+JNIEXPORT jint JNICALL
+Java_java_util_zip_Adler32_updateByteBuffer(JNIEnv *env, jclass cls, jint adler,
+                                            jlong address, jint off, jint len)
+{
+    Bytef *buf = (Bytef *)jlong_to_ptr(address);
+    if (buf) {
+        adler = adler32(adler, buf + off, len);
+    }
+    return adler;
+}