OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / java / security / MessageDigest.java
index b817759..0f8e934 100644 (file)
@@ -38,6 +38,9 @@ exception statement from your version. */
 package java.security;
 
 import gnu.java.security.Engine;
+import java.nio.ByteBuffer;
+
+import java.lang.reflect.InvocationTargetException;
 
 /**
  * Message digests are secure one-way hash functions that take arbitrary-sized
@@ -72,28 +75,29 @@ public abstract class MessageDigest extends MessageDigestSpi
    * Returns a new instance of <code>MessageDigest</code> representing the
    * specified algorithm.
    * 
-   * @param algorithm
-   *          the name of the digest algorithm to use.
+   * @param algorithm the name of the digest algorithm to use.
    * @return a new instance representing the desired algorithm.
-   * @throws NoSuchAlgorithmException
-   *           if the algorithm is not implemented by any provider.
+   * @throws NoSuchAlgorithmException if the algorithm is not implemented by any
+   *           provider.
+   * @throws IllegalArgumentException if <code>algorithm</code> is
+   *           <code>null</code> or is an empty string.
    */
   public static MessageDigest getInstance(String algorithm)
-    throws NoSuchAlgorithmException
+      throws NoSuchAlgorithmException
   {
     Provider[] p = Security.getProviders();
+    NoSuchAlgorithmException lastException = null;
     for (int i = 0; i < p.length; i++)
-      {
-        try
-          {
-            return getInstance(algorithm, p[i]);
-          }
-        catch (NoSuchAlgorithmException ignored)
-         {
-           // Ignore.
-         }
-      }
-
+      try
+        {
+          return getInstance(algorithm, p[i]);
+        }
+      catch (NoSuchAlgorithmException x)
+        {
+          lastException = x;
+        }
+    if (lastException != null)
+      throw lastException;
     throw new NoSuchAlgorithmException(algorithm);
   }
 
@@ -101,29 +105,26 @@ public abstract class MessageDigest extends MessageDigestSpi
    * Returns a new instance of <code>MessageDigest</code> representing the
    * specified algorithm from a named provider.
    * 
-   * @param algorithm
-   *          the name of the digest algorithm to use.
-   * @param provider
-   *          the name of the provider to use.
+   * @param algorithm the name of the digest algorithm to use.
+   * @param provider the name of the provider to use.
    * @return a new instance representing the desired algorithm.
-   * @throws NoSuchAlgorithmException
-   *           if the algorithm is not implemented by the named provider.
-   * @throws NoSuchProviderException
-   *           if the named provider was not found.
+   * @throws NoSuchAlgorithmException if the algorithm is not implemented by the
+   *           named provider.
+   * @throws NoSuchProviderException if the named provider was not found.
+   * @throws IllegalArgumentException if either <code>algorithm</code> or
+   *           <code>provider</code> is <code>null</code> or empty.
    */
   public static MessageDigest getInstance(String algorithm, String provider)
-    throws NoSuchAlgorithmException, NoSuchProviderException
+      throws NoSuchAlgorithmException, NoSuchProviderException
   {
-    if (provider != null)
-      provider = provider.trim();
-
-    if (provider == null || provider.length() == 0)
-      throw new IllegalArgumentException("Illegal provider");
-
+    if (provider == null)
+      throw new IllegalArgumentException("provider MUST NOT be null");
+    provider = provider.trim();
+    if (provider.length() == 0)
+      throw new IllegalArgumentException("provider MUST NOT be empty");
     Provider p = Security.getProvider(provider);
     if (p == null)
       throw new NoSuchProviderException(provider);
-
     return getInstance(algorithm, p);
   }
 
@@ -131,39 +132,43 @@ public abstract class MessageDigest extends MessageDigestSpi
    * Returns a new instance of <code>MessageDigest</code> representing the
    * specified algorithm from a designated {@link Provider}.
    * 
-   * @param algorithm
-   *          the name of the digest algorithm to use.
-   * @param provider
-   *          the {@link Provider} to use.
+   * @param algorithm the name of the digest algorithm to use.
+   * @param provider the {@link Provider} to use.
    * @return a new instance representing the desired algorithm.
-   * @throws IllegalArgumentException
-   *           if <code>provider</code> is <code>null</code>.
-   * @throws NoSuchAlgorithmException
-   *           if the algorithm is not implemented by {@link Provider}.
+   * @throws NoSuchAlgorithmException if the algorithm is not implemented by
+   *           {@link Provider}.
+   * @throws IllegalArgumentException if either <code>algorithm</code> or
+   *           <code>provider</code> is <code>null</code>, or if
+   *           <code>algorithm</code> is an empty string.
    * @since 1.4
    * @see Provider
    */
   public static MessageDigest getInstance(String algorithm, Provider provider)
     throws NoSuchAlgorithmException
   {
-    if (provider == null)
-      throw new IllegalArgumentException("Illegal provider");
-
-    MessageDigest result = null;
-    Object o = null;
+    StringBuilder sb = new StringBuilder("MessageDigest for algorithm [")
+        .append(algorithm).append("] from provider[")
+        .append(provider).append("] ");
+    Object o;
     try
       {
         o = Engine.getInstance(MESSAGE_DIGEST, algorithm, provider);
       }
-    catch (java.lang.reflect.InvocationTargetException ite)
+    catch (InvocationTargetException x)
       {
-        throw new NoSuchAlgorithmException(algorithm);
+        Throwable cause = x.getCause();
+        if (cause instanceof NoSuchAlgorithmException)
+          throw (NoSuchAlgorithmException) cause;
+        if (cause == null)
+          cause = x;
+        sb.append("could not be created");
+        NoSuchAlgorithmException y = new NoSuchAlgorithmException(sb.toString());
+        y.initCause(cause);
+        throw y;
       }
-
+    MessageDigest result;
     if (o instanceof MessageDigestSpi)
-      {
-        result = new DummyMessageDigest((MessageDigestSpi) o, algorithm);
-      }
+      result = new DummyMessageDigest((MessageDigestSpi) o, algorithm);
     else if (o instanceof MessageDigest)
       {
         result = (MessageDigest) o;
@@ -171,7 +176,8 @@ public abstract class MessageDigest extends MessageDigestSpi
       }
     else
       {
-        throw new NoSuchAlgorithmException(algorithm);
+        sb.append("is of an unexpected Type: ").append(o.getClass().getName());
+        throw new NoSuchAlgorithmException(sb.toString());
       }
     result.provider = provider;
     return result;
@@ -224,6 +230,17 @@ public abstract class MessageDigest extends MessageDigestSpi
   }
 
   /**
+   * Updates the digest with the remaining bytes of a buffer.
+   * 
+   * @param input The input byte buffer.
+   * @since 1.5
+   */
+  public void update (ByteBuffer input)
+  {
+    engineUpdate (input);
+  }
+  
+  /**
    * Computes the final digest of the stored data.
    * 
    * @return a byte array representing the message digest.