Commits

Anonymous committed ea7e28b

MessageDigest not thread safe, synchronized added
Issue number: CACHE-183
Obtained from: Lars Torunski

Comments (0)

Files changed (1)

src/java/com/opensymphony/oscache/plugins/diskpersistence/HashDiskPersistenceListener.java

      * @param key cache entry key
      * @return char[] file name
      */
-    protected char[] getCacheFileName(String key) {
-        byte[] digest;
-
+    protected synchronized char[] getCacheFileName(String key) {
         if ((key == null) || (key.length() == 0)) {
             throw new IllegalArgumentException("Invalid key '" + key + "' specified to getCacheFile.");
         }
 
-        digest = md.digest(key.getBytes());
+        byte[] digest = md.digest(key.getBytes());
 
-        String hexString = byteArrayToHexString(digest);
-        return hexString.toCharArray();
+        return byteArrayToHexString(digest).toCharArray();
     }
 
     /**
      * @return a java.lang.String based version of they byte array
      */
     static String byteArrayToHexString(byte[] in) {
-        byte ch = 0x00;
-        int i = 0;
-
         if ((in == null) || (in.length <= 0)) {
             return null;
         }
 
-        String[] pseudo = {
-            "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D",
-            "E", "F"
-        };
         StringBuffer out = new StringBuffer(in.length * 2);
-
-        while (i < in.length) {
-            ch = (byte) (in[i] & 0xF0); // Strip off high nibble
+        
+        for (int i = 0; i < in.length; i++) {
+            byte ch = (byte) (in[i] & 0xF0); // Strip off high nibble
             ch = (byte) (ch >>> 4);
 
             // shift the bits down
             ch = (byte) (ch & 0x0F);
 
             //	 must do this is high order bit is on!
-            out.append(pseudo[(int) ch]); // convert the nibble to a String Character
+            out.append(PSEUDO[(int) ch]); // convert the nibble to a String Character
             ch = (byte) (in[i] & 0x0F); // Strip off low nibble 
-            out.append(pseudo[(int) ch]); // convert the nibble to a String Character
+            out.append(PSEUDO[(int) ch]); // convert the nibble to a String Character
             i++;
         }
 
-        String rslt = new String(out);
-        return rslt;
+        return out.toString();
     }
+    
+    static final String[] PSEUDO = {
+            "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D",
+            "E", "F"
+        };
+
 }