shiro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lhazlew...@apache.org
Subject svn commit: r746882 - /incubator/jsecurity/trunk/core/src/org/jsecurity/codec/CodecSupport.java
Date Mon, 23 Feb 2009 05:01:12 GMT
Author: lhazlewood
Date: Mon Feb 23 05:01:11 2009
New Revision: 746882

URL: http://svn.apache.org/viewvc?rev=746882&view=rev
Log:
Added the ability to hash a File and InputStream

Modified:
    incubator/jsecurity/trunk/core/src/org/jsecurity/codec/CodecSupport.java

Modified: incubator/jsecurity/trunk/core/src/org/jsecurity/codec/CodecSupport.java
URL: http://svn.apache.org/viewvc/incubator/jsecurity/trunk/core/src/org/jsecurity/codec/CodecSupport.java?rev=746882&r1=746881&r2=746882&view=diff
==============================================================================
--- incubator/jsecurity/trunk/core/src/org/jsecurity/codec/CodecSupport.java (original)
+++ incubator/jsecurity/trunk/core/src/org/jsecurity/codec/CodecSupport.java Mon Feb 23 05:01:11
2009
@@ -18,11 +18,11 @@
  */
 package org.jsecurity.codec;
 
-import java.io.UnsupportedEncodingException;
+import java.io.*;
 
 /**
  * Base abstract class that provides useful encoding and decoding operations, especially
for character data.
- * 
+ *
  * @author Les Hazlewood
  * @since 0.9
  */
@@ -167,6 +167,10 @@
             return toBytes((char[]) o);
         } else if (o instanceof String) {
             return toBytes((String) o);
+        } else if (o instanceof File) {
+            return toBytes((File)o);
+        } else if (o instanceof InputStream) {
+            return toBytes((InputStream) o);
         } else {
             return objectToBytes(o);
         }
@@ -200,6 +204,54 @@
         }
     }
 
+    protected byte[] toBytes(File file) {
+        if (file == null) {
+            throw new IllegalArgumentException("File argument cannot be null.");
+        }
+        try {
+            return toBytes(new FileInputStream(file));
+        } catch (FileNotFoundException e) {
+            String msg = "Unable to acquire InputStream for file [" + file + "]";
+            throw new CodecException(msg, e);
+        }
+    }
+
+    /**
+     * Converts the specified {@link InputStream InputStream} into a byte array.
+     *
+     * @param in the InputStream to convert to a byte array
+     * @return the bytes of the input stream
+     * @throws IllegalArgumentException if the {@code InputStream} argument is {@code null}.
+     * @throws CodecException           if there is any problem reading from the {@link InputStream}.
+     * @since 1.0
+     */
+    protected byte[] toBytes(InputStream in) {
+        if (in == null) {
+            throw new IllegalArgumentException("InputStream argument cannot be null.");
+        }
+        final int BUFFER_SIZE = 512;
+        ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
+        byte[] buffer = new byte[BUFFER_SIZE];
+        int bytesRead;
+        try {
+            while ((bytesRead = in.read(buffer)) != -1) {
+                out.write(buffer, 0, bytesRead);
+            }
+            return out.toByteArray();
+        } catch (IOException ioe) {
+            throw new CodecException(ioe);
+        } finally {
+            try {
+                in.close();
+            } catch (IOException ignored) {
+            }
+            try {
+                out.close();
+            } catch (IOException ignored) {
+            }
+        }
+    }
+
     /**
      * Default implementation throws a CodecException immediately since it can't infer how
to convert the Object
      * to a byte array.  This method must be overridden by subclasses if anything other than
the three default



Mime
View raw message