8049225: Source class exposes public mutable array
authorattila
Thu, 03 Jul 2014 11:18:38 +0200
changeset 25255 cf56cdc2c663
parent 25254 a56c2713f1a8
child 25256 90ab6e5bb5dd
8049225: Source class exposes public mutable array Reviewed-by: hannesw, sundar
nashorn/src/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java
nashorn/src/jdk/nashorn/internal/runtime/CodeStore.java
nashorn/src/jdk/nashorn/internal/runtime/Source.java
--- a/nashorn/src/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java	Thu Jul 03 11:18:26 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java	Thu Jul 03 11:18:38 2014 +0200
@@ -78,7 +78,7 @@
         }
         final StringBuilder b = new StringBuilder(48);
         // Base64-encode the digest of the source, and append the function id.
-        b.append(Base64.getUrlEncoder().encodeToString(source.getDigest())).append('-').append(functionId);
+        b.append(source.getDigest()).append('-').append(functionId);
         // Finally, if this is a parameter-type specialized version of the function, add the parameter types to the file
         // name.
         if(paramTypes != null && paramTypes.length > 0) {
@@ -286,7 +286,7 @@
                 for(;;) {
                     final int l = in.read(buf);
                     if(l == -1) {
-                        return Base64.getUrlEncoder().encodeToString(digest.digest());
+                        return Base64.getUrlEncoder().withoutPadding().encodeToString(digest.digest());
                     }
                     digest.update(buf, 0, l);
                 }
--- a/nashorn/src/jdk/nashorn/internal/runtime/CodeStore.java	Thu Jul 03 11:18:26 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/CodeStore.java	Thu Jul 03 11:18:38 2014 +0200
@@ -37,7 +37,6 @@
 import java.security.AccessController;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
-import java.util.Base64;
 import java.util.Map;
 
 /**
@@ -48,9 +47,6 @@
     private final File dir;
     private final int minSize;
 
-    // Message digest to file name encoder
-    private final static Base64.Encoder BASE64 = Base64.getUrlEncoder().withoutPadding();
-
     // Default minimum size for storing a compiled script class
     private final static int DEFAULT_MIN_SIZE = 1000;
 
@@ -108,8 +104,7 @@
             return null;
         }
 
-        final String digest = BASE64.encodeToString(source.getDigest());
-        final File file = new File(dir, digest);
+        final File file = new File(dir, source.getDigest());
 
         try {
             return AccessController.doPrivileged(new PrivilegedExceptionAction<CompiledScript>() {
@@ -157,8 +152,7 @@
             }
         }
 
-        final String digest = BASE64.encodeToString(source.getDigest());
-        final File file = new File(dir, digest);
+        final File file = new File(dir, source.getDigest());
         final CompiledScript script = new CompiledScript(source, mainClassName, classBytes, constants);
 
         try {
--- a/nashorn/src/jdk/nashorn/internal/runtime/Source.java	Thu Jul 03 11:18:26 2014 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/Source.java	Thu Jul 03 11:18:38 2014 +0200
@@ -45,6 +45,7 @@
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
+import java.util.Base64;
 import java.util.Objects;
 import java.util.WeakHashMap;
 import jdk.nashorn.api.scripting.URLReader;
@@ -52,7 +53,6 @@
 import jdk.nashorn.internal.runtime.logging.DebugLogger;
 import jdk.nashorn.internal.runtime.logging.Loggable;
 import jdk.nashorn.internal.runtime.logging.Logger;
-
 /**
  * Source objects track the origin of JavaScript entities.
  */
@@ -61,6 +61,9 @@
     private static final int BUF_SIZE = 8 * 1024;
     private static final Cache CACHE = new Cache();
 
+    // Message digest to file name encoder
+    private final static Base64.Encoder BASE64 = Base64.getUrlEncoder().withoutPadding();
+
     /**
      * Descriptive name of the source as supplied by the user. Used for error
      * reporting to the user. For example, SyntaxError will use this to print message.
@@ -81,8 +84,8 @@
     /** Cached hash code */
     private int hash;
 
-    /** Message digest */
-    private byte[] digest;
+    /** Base64-encoded SHA1 digest of this source object */
+    private volatile byte[] digest;
 
     // Do *not* make this public, ever! Trusts the URL and content.
     private Source(final String name, final String base, final Data data) {
@@ -782,12 +785,17 @@
     }
 
     /**
-     * Get a message digest for this source.
+     * Get a Base64-encoded SHA1 digest for this source.
      *
-     * @return a message digest for this source
+     * @return a Base64-encoded SHA1 digest for this source
      */
-    public synchronized byte[] getDigest() {
-        if (digest == null) {
+    public String getDigest() {
+        return new String(getDigestBytes(), StandardCharsets.US_ASCII);
+    }
+
+    private byte[] getDigestBytes() {
+        byte[] ldigest = digest;
+        if (ldigest == null) {
             final char[] content = data();
             final byte[] bytes = new byte[content.length * 2];
 
@@ -807,12 +815,12 @@
                 if (getURL() != null) {
                     md.update(getURL().toString().getBytes(StandardCharsets.UTF_8));
                 }
-                digest = md.digest(bytes);
+                digest = ldigest = BASE64.encode(md.digest(bytes));
             } catch (final NoSuchAlgorithmException e) {
                 throw new RuntimeException(e);
             }
         }
-        return digest;
+        return ldigest;
     }
 
     /**