Skip to content

Commit

Permalink
Modified to use ByteBufferRentalService
Browse files Browse the repository at this point in the history
  • Loading branch information
zanella committed Nov 16, 2016
1 parent 9cc01de commit 3f99894
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions core/src/main/java/com/turn/ttorrent/common/Torrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -676,21 +676,25 @@ private static class CallableChunkHasher implements Callable<String> {

private final MessageDigest md;
private final ByteBuffer data;
private final ByteBufferRentalService bbrs;

CallableChunkHasher(ByteBuffer buffer) throws NoSuchAlgorithmException {
CallableChunkHasher(ByteBuffer rentedBuffer, ByteBufferRentalService bbrs) throws NoSuchAlgorithmException {
this.md = MessageDigest.getInstance("SHA-1");

this.data = ByteBuffer.allocate(buffer.remaining());
buffer.mark();
this.data.put(buffer);
this.data.clear();
buffer.reset();
rentedBuffer.mark();
rentedBuffer.reset();
this.data = rentedBuffer;

this.bbrs = bbrs;
}

@Override
public String call() throws UnsupportedEncodingException {
public String call() throws UnsupportedEncodingException, InterruptedException {
this.md.reset();
this.md.update(this.data.array());
this.md.update(this.data);

bbrs.put( this.data );

return new String(md.digest(), Torrent.BYTE_ENCODING);
}
}
Expand Down Expand Up @@ -719,13 +723,14 @@ private static String hashFiles(List<File> files, int pieceLenght)
throws InterruptedException, IOException, NoSuchAlgorithmException {
int threads = getHashingThreadsCount();
ExecutorService executor = Executors.newFixedThreadPool(threads);
ByteBuffer buffer = ByteBuffer.allocate(pieceLenght);
final ByteBufferRentalService bbrs = new ByteBufferRentalService(threads + 1, pieceLenght);
List<Future<String>> results = new LinkedList<Future<String>>();
StringBuilder hashes = new StringBuilder();

long length = 0L;
int pieces = 0;

ByteBuffer buffer = null;
long start = System.nanoTime();
for (File file : files) {
logger.info("Hashing data from {} with {} threads ({} pieces)...",
Expand All @@ -743,10 +748,13 @@ private static String hashFiles(List<File> files, int pieceLenght)
int step = 10;

try {
buffer = bbrs.take();

while (channel.read(buffer) > 0) {
if (buffer.remaining() == 0) {
buffer.clear();
results.add(executor.submit(new CallableChunkHasher(buffer)));
results.add(executor.submit(new CallableChunkHasher(buffer, bbrs)));
buffer = bbrs.take();
}

if (results.size() >= threads) {
Expand All @@ -765,10 +773,10 @@ private static String hashFiles(List<File> files, int pieceLenght)
}

// Hash the last bit, if any
if (buffer.position() > 0) {
if ((buffer != null) && (buffer.position() > 0)) {
buffer.limit(buffer.position());
buffer.position(0);
results.add(executor.submit(new CallableChunkHasher(buffer)));
results.add(executor.submit(new CallableChunkHasher(buffer, bbrs)));
}

pieces += accumulateHashes(hashes, results);
Expand Down

0 comments on commit 3f99894

Please sign in to comment.