Added the ability to hash a File and InputStream
git-svn-id: https://svn.apache.org/repos/asf/incubator/jsecurity/trunk@746882 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/core/src/org/jsecurity/codec/CodecSupport.java b/core/src/org/jsecurity/codec/CodecSupport.java
index 17846bf..5ddd281 100644
--- a/core/src/org/jsecurity/codec/CodecSupport.java
+++ b/core/src/org/jsecurity/codec/CodecSupport.java
@@ -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