Moved NamedThreadFactory and ByteArrayWrapper to util project. Changed try/catch with hashing files in Crypto.Util

This commit is contained in:
nathan 2014-08-25 18:43:01 +02:00
parent c66c1d43df
commit e6ab2bef0e
3 changed files with 101 additions and 13 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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];
}