Skip to content

Commit 3f99894

Browse files
committed
Modified to use ByteBufferRentalService
1 parent 9cc01de commit 3f99894

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

core/src/main/java/com/turn/ttorrent/common/Torrent.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -676,21 +676,25 @@ private static class CallableChunkHasher implements Callable<String> {
676676

677677
private final MessageDigest md;
678678
private final ByteBuffer data;
679+
private final ByteBufferRentalService bbrs;
679680

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

683-
this.data = ByteBuffer.allocate(buffer.remaining());
684-
buffer.mark();
685-
this.data.put(buffer);
686-
this.data.clear();
687-
buffer.reset();
684+
rentedBuffer.mark();
685+
rentedBuffer.reset();
686+
this.data = rentedBuffer;
687+
688+
this.bbrs = bbrs;
688689
}
689690

690691
@Override
691-
public String call() throws UnsupportedEncodingException {
692+
public String call() throws UnsupportedEncodingException, InterruptedException {
692693
this.md.reset();
693-
this.md.update(this.data.array());
694+
this.md.update(this.data);
695+
696+
bbrs.put( this.data );
697+
694698
return new String(md.digest(), Torrent.BYTE_ENCODING);
695699
}
696700
}
@@ -719,13 +723,14 @@ private static String hashFiles(List<File> files, int pieceLenght)
719723
throws InterruptedException, IOException, NoSuchAlgorithmException {
720724
int threads = getHashingThreadsCount();
721725
ExecutorService executor = Executors.newFixedThreadPool(threads);
722-
ByteBuffer buffer = ByteBuffer.allocate(pieceLenght);
726+
final ByteBufferRentalService bbrs = new ByteBufferRentalService(threads + 1, pieceLenght);
723727
List<Future<String>> results = new LinkedList<Future<String>>();
724728
StringBuilder hashes = new StringBuilder();
725729

726730
long length = 0L;
727731
int pieces = 0;
728732

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

745750
try {
751+
buffer = bbrs.take();
752+
746753
while (channel.read(buffer) > 0) {
747754
if (buffer.remaining() == 0) {
748755
buffer.clear();
749-
results.add(executor.submit(new CallableChunkHasher(buffer)));
756+
results.add(executor.submit(new CallableChunkHasher(buffer, bbrs)));
757+
buffer = bbrs.take();
750758
}
751759

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

767775
// Hash the last bit, if any
768-
if (buffer.position() > 0) {
776+
if ((buffer != null) && (buffer.position() > 0)) {
769777
buffer.limit(buffer.position());
770778
buffer.position(0);
771-
results.add(executor.submit(new CallableChunkHasher(buffer)));
779+
results.add(executor.submit(new CallableChunkHasher(buffer, bbrs)));
772780
}
773781

774782
pieces += accumulateHashes(hashes, results);

0 commit comments

Comments
 (0)