Add Package splitterng.utils
authorMaximilian Quellmalz <maximilian.quellmalz@mailbox.tu-dresden.de>
Thu, 23 Oct 2014 14:08:44 +0000 (16:08 +0200)
committerMaximilian Quellmalz <maximilian.quellmalz@mailbox.tu-dresden.de>
Thu, 23 Oct 2014 14:08:44 +0000 (16:08 +0200)
Add ByteUtils
Add FileUtils

Splitter-ng/src/splitterng/utils/ByteUtils.java [new file with mode: 0644]
Splitter-ng/src/splitterng/utils/FileUtils.java [new file with mode: 0644]

diff --git a/Splitter-ng/src/splitterng/utils/ByteUtils.java b/Splitter-ng/src/splitterng/utils/ByteUtils.java
new file mode 100644 (file)
index 0000000..6e41aaf
--- /dev/null
@@ -0,0 +1,86 @@
+package splitterng.utils;
+
+public class ByteUtils {
+    private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5',
+           '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
+
+
+    /**
+     * Convert a string containing hexadecimal characters to a byte-array.
+     * 
+     * @param s
+     *                a hex string
+     * @return a byte array with the corresponding value
+     */
+    public static byte[] fromHexString(String s) {
+       char[] rawChars = s.toUpperCase().toCharArray();
+
+       int hexChars = 0;
+       for (int i = 0; i < rawChars.length; i++) {
+           if ((rawChars[i] >= '0' && rawChars[i] <= '9')
+                   || (rawChars[i] >= 'A' && rawChars[i] <= 'F')) {
+               hexChars++;
+           }
+       }
+
+       byte[] byteString = new byte[(hexChars + 1) >> 1];
+
+       int pos = hexChars & 1;
+
+       for (int i = 0; i < rawChars.length; i++) {
+           if (rawChars[i] >= '0' && rawChars[i] <= '9') {
+               byteString[pos >> 1] <<= 4;
+               byteString[pos >> 1] |= rawChars[i] - '0';
+           } else if (rawChars[i] >= 'A' && rawChars[i] <= 'F') {
+               byteString[pos >> 1] <<= 4;
+               byteString[pos >> 1] |= rawChars[i] - 'A' + 10;
+           } else {
+               continue;
+           }
+           pos++;
+       }
+
+       return byteString;
+    }
+
+    /**
+     * Convert a byte array to the corresponding hexstring.
+     * 
+     * @param input
+     *                the byte array to be converted
+     * @return the corresponding hexstring
+     */
+    public static String toHexString(byte[] input) {
+       String result = "";
+       for (int i = 0; i < input.length; i++) {
+           result += HEX_CHARS[(input[i] >>> 4) & 0x0f];
+           result += HEX_CHARS[(input[i]) & 0x0f];
+       }
+       return result;
+    }
+
+    /**
+     * Convert a byte array to the corresponding hex string.
+     * 
+     * @param input
+     *                the byte array to be converted
+     * @param prefix
+     *                the prefix to put at the beginning of the hex string
+     * @param seperator
+     *                a separator string
+     * @return the corresponding hex string
+     */
+    public static String toHexString(byte[] input, String prefix,
+           String seperator) {
+       String result = new String(prefix);
+       for (int i = 0; i < input.length; i++) {
+           result += HEX_CHARS[(input[i] >>> 4) & 0x0f];
+           result += HEX_CHARS[(input[i]) & 0x0f];
+           if (i < input.length - 1) {
+               result += seperator;
+           }
+       }
+       return result;
+    }
+
+}
diff --git a/Splitter-ng/src/splitterng/utils/FileUtils.java b/Splitter-ng/src/splitterng/utils/FileUtils.java
new file mode 100644 (file)
index 0000000..e554bcb
--- /dev/null
@@ -0,0 +1,28 @@
+package splitterng.utils;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+public class FileUtils {
+       public static String hashFile(File file, String algorithm) throws FileNotFoundException, IOException, NoSuchAlgorithmException {
+           try (FileInputStream inputStream = new FileInputStream(file)) {
+               MessageDigest digest = MessageDigest.getInstance(algorithm);
+        
+               byte[] bytesBuffer = new byte[4096];
+               int bytesRead = -1;
+        
+               while ((bytesRead = inputStream.read(bytesBuffer)) != -1) {
+                   digest.update(bytesBuffer, 0, bytesRead);
+               }
+        
+               byte[] hashedBytes = digest.digest();
+        
+               return ByteUtils.toHexString(hashedBytes);
+           } 
+           
+       }
+}