From e6ab2bef0e88044dfb11972c766242343e86df6f Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 25 Aug 2014 18:43:01 +0200 Subject: [PATCH] Moved NamedThreadFactory and ByteArrayWrapper to util project. Changed try/catch with hashing files in Crypto.Util --- .../src/dorkbox/util/NamedThreadFactory.java | 42 +++++++++++++++++++ .../dorkbox/util/bytes/ByteArrayWrapper.java | 37 ++++++++++++++++ .../src/dorkbox/util/crypto/Crypto.java | 35 ++++++++++------ 3 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 Dorkbox-Util/src/dorkbox/util/NamedThreadFactory.java create mode 100644 Dorkbox-Util/src/dorkbox/util/bytes/ByteArrayWrapper.java diff --git a/Dorkbox-Util/src/dorkbox/util/NamedThreadFactory.java b/Dorkbox-Util/src/dorkbox/util/NamedThreadFactory.java new file mode 100644 index 0000000..1be579e --- /dev/null +++ b/Dorkbox-Util/src/dorkbox/util/NamedThreadFactory.java @@ -0,0 +1,42 @@ +package dorkbox.util; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * The default thread factory with names. + */ +public class NamedThreadFactory implements ThreadFactory { + private static final AtomicInteger poolId = new AtomicInteger(); + // permit this to be changed! + /** + * Stack size must be specified in bytes. Default is 8k + */ + public static int stackSizeForNettyThreads = 8192; + + private final AtomicInteger nextId = new AtomicInteger(); + + final ThreadGroup group; + final String namePrefix; + + public NamedThreadFactory(String poolNamePrefix, ThreadGroup group) { + this.group = group; + namePrefix = poolNamePrefix + '-' + poolId.incrementAndGet(); + } + + @Override + public Thread newThread(Runnable r) { + // stack size is arbitrary based on JVM implementation. Default is 0 + // 8k is the size of the android stack. Depending on the version of android, this can either change, or will always be 8k + // To be honest, 8k is pretty reasonable for an asynchronous/event based system (32bit) or 16k (64bit) + // Setting the size MAY or MAY NOT have any effect!!! + Thread t = new Thread(group, r, namePrefix + '-' + nextId.incrementAndGet(), stackSizeForNettyThreads); + if (!t.isDaemon()) { + t.setDaemon(true); + } + if (t.getPriority() != Thread.MAX_PRIORITY) { + t.setPriority(Thread.MAX_PRIORITY); + } + return t; + } +} \ No newline at end of file diff --git a/Dorkbox-Util/src/dorkbox/util/bytes/ByteArrayWrapper.java b/Dorkbox-Util/src/dorkbox/util/bytes/ByteArrayWrapper.java new file mode 100644 index 0000000..42cb3b6 --- /dev/null +++ b/Dorkbox-Util/src/dorkbox/util/bytes/ByteArrayWrapper.java @@ -0,0 +1,37 @@ +package dorkbox.util.bytes; + +import java.util.Arrays; + +/** + * Necessary to provide equals and hashcode methods on a byte arrays, if they are to be used as keys in a map/set/etc + */ +public final class ByteArrayWrapper { + private final byte[] data; + + public ByteArrayWrapper(byte[] data) { + if (data == null) { + throw new NullPointerException(); + } + int length = data.length; + this.data = new byte[length]; + // copy so it's immutable as a key. + System.arraycopy(data, 0, this.data, 0, length); + } + + public byte[] getBytes() { + return this.data; + } + + @Override + public boolean equals(Object other) { + if (!(other instanceof ByteArrayWrapper)) { + return false; + } + return Arrays.equals(this.data, ((ByteArrayWrapper) other).data); + } + + @Override + public int hashCode() { + return Arrays.hashCode(this.data); + } +} \ No newline at end of file diff --git a/Dorkbox-Util/src/dorkbox/util/crypto/Crypto.java b/Dorkbox-Util/src/dorkbox/util/crypto/Crypto.java index 0497867..6989ec2 100644 --- a/Dorkbox-Util/src/dorkbox/util/crypto/Crypto.java +++ b/Dorkbox-Util/src/dorkbox/util/crypto/Crypto.java @@ -106,23 +106,24 @@ public class Crypto { /** * Return the hash of the file or NULL if file is invalid */ - public static final byte[] hashFile(File file, Digest digest) throws IOException { + public static final byte[] hashFile(File file, Digest digest) { return hashFile(file, digest, 0L); } /** * Return the hash of the file or NULL if file is invalid */ - public static final byte[] hashFile(File file, Digest digest, long lengthFromEnd) throws IOException { + public static final byte[] hashFile(File file, Digest digest, long lengthFromEnd) { if (file.isFile() && file.canRead()) { - InputStream inputStream = new FileInputStream(file); - long size = file.length(); - - if (lengthFromEnd > 0 && lengthFromEnd < size) { - size -= lengthFromEnd; - } - + InputStream inputStream = null; try { + inputStream = new FileInputStream(file); + long size = file.length(); + + if (lengthFromEnd > 0 && lengthFromEnd < size) { + size -= lengthFromEnd; + } + int bufferSize = 4096; byte[] buffer = new byte[bufferSize]; @@ -141,8 +142,16 @@ public class Crypto { digest.update(buffer, 0, readBytes); } + } catch (IOException e) { + logger.error("Error hashing file: {}", file.getAbsolutePath(), e); } finally { - inputStream.close(); + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } byte[] digestBytes = new byte[digest.getDigestSize()]; @@ -793,13 +802,13 @@ public class Crypto { try { actualLength += aesEngine.doFinal(outBuf, actualLength); } catch (DataLengthException e) { - logger.error("Unable to perform AES cipher.", e); + logger.debug("Unable to perform AES cipher.", e); return new byte[0]; } catch (IllegalStateException e) { - logger.error("Unable to perform AES cipher.", e); + logger.debug("Unable to perform AES cipher.", e); return new byte[0]; } catch (InvalidCipherTextException e) { - logger.error("Unable to perform AES cipher.", e); + logger.debug("Unable to perform AES cipher.", e); return new byte[0]; }