Skip to content

Commit 500062b

Browse files
committed
Using Commons Pool
1 parent 3f99894 commit 500062b

File tree

4 files changed

+73
-57
lines changed

4 files changed

+73
-57
lines changed

core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<artifactId>commons-io</artifactId>
1818
<version>2.4</version>
1919
</dependency>
20+
<dependency>
21+
<groupId>org.apache.commons</groupId>
22+
<artifactId>commons-pool2</artifactId>
23+
<version>2.4.2</version>
24+
</dependency>
2025
<dependency>
2126
<groupId>org.simpleframework</groupId>
2227
<artifactId>simple</artifactId>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.turn.ttorrent.common;
2+
3+
import org.apache.commons.pool2.BasePooledObjectFactory;
4+
import org.apache.commons.pool2.PooledObject;
5+
import org.apache.commons.pool2.impl.DefaultPooledObject;
6+
import org.apache.commons.pool2.impl.GenericObjectPool;
7+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
8+
9+
import java.nio.ByteBuffer;
10+
11+
public class ByteBufferPool {
12+
13+
private final GenericObjectPool<ByteBuffer> pool;
14+
15+
public GenericObjectPool<ByteBuffer> getPool() {
16+
return pool;
17+
}
18+
19+
public ByteBufferPool(int amount, int length) {
20+
final GenericObjectPoolConfig gopc = new GenericObjectPoolConfig();
21+
gopc.setMinIdle(amount);
22+
gopc.setMaxTotal(amount);
23+
24+
pool = new GenericObjectPool<ByteBuffer>(new ByteBufferFactory(length), gopc);
25+
}
26+
27+
private class ByteBufferFactory extends BasePooledObjectFactory<ByteBuffer> {
28+
private final int length;
29+
30+
private ByteBufferFactory(int length) {
31+
super();
32+
33+
this.length = length;
34+
}
35+
36+
@Override
37+
public ByteBuffer create() {
38+
return ByteBuffer.allocate(length);
39+
}
40+
41+
@Override
42+
public PooledObject<ByteBuffer> wrap(ByteBuffer buffer) {
43+
return new DefaultPooledObject<ByteBuffer>(buffer);
44+
}
45+
46+
@Override
47+
public void passivateObject(PooledObject<ByteBuffer> pooledObject) {
48+
pooledObject.getObject().clear();
49+
}
50+
}
51+
}

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

Lines changed: 0 additions & 41 deletions
This file was deleted.

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ public static Torrent load(File torrent, boolean seeder)
505505
* torrent's creator.
506506
*/
507507
public static Torrent create(File source, URI announce, String createdBy)
508-
throws InterruptedException, IOException, NoSuchAlgorithmException {
508+
throws InterruptedException, IOException, NoSuchAlgorithmException, Exception {
509509
return Torrent.create(source, null, DEFAULT_PIECE_LENGTH,
510510
announce, null, createdBy);
511511
}
@@ -528,7 +528,7 @@ public static Torrent create(File source, URI announce, String createdBy)
528528
* torrent's creator.
529529
*/
530530
public static Torrent create(File parent, List<File> files, URI announce,
531-
String createdBy) throws InterruptedException, IOException, NoSuchAlgorithmException {
531+
String createdBy) throws InterruptedException, IOException, NoSuchAlgorithmException, Exception {
532532
return Torrent.create(parent, files, DEFAULT_PIECE_LENGTH,
533533
announce, null, createdBy);
534534
}
@@ -549,7 +549,7 @@ public static Torrent create(File parent, List<File> files, URI announce,
549549
* torrent's creator.
550550
*/
551551
public static Torrent create(File source, int pieceLength, List<List<URI>> announceList,
552-
String createdBy) throws InterruptedException, IOException, NoSuchAlgorithmException {
552+
String createdBy) throws InterruptedException, IOException, NoSuchAlgorithmException, Exception {
553553
return Torrent.create(source, null, pieceLength,
554554
null, announceList, createdBy);
555555
}
@@ -574,7 +574,7 @@ public static Torrent create(File source, int pieceLength, List<List<URI>> annou
574574
*/
575575
public static Torrent create(File source, List<File> files, int pieceLength,
576576
List<List<URI>> announceList, String createdBy)
577-
throws InterruptedException, IOException, NoSuchAlgorithmException {
577+
throws InterruptedException, IOException, NoSuchAlgorithmException, Exception {
578578
return Torrent.create(source, files, pieceLength,
579579
null, announceList, createdBy);
580580
}
@@ -600,7 +600,7 @@ public static Torrent create(File source, List<File> files, int pieceLength,
600600
*/
601601
private static Torrent create(File parent, List<File> files, int pieceLength,
602602
URI announce, List<List<URI>> announceList, String createdBy)
603-
throws InterruptedException, IOException, NoSuchAlgorithmException {
603+
throws InterruptedException, IOException, NoSuchAlgorithmException, Exception {
604604
if (files == null || files.isEmpty()) {
605605
logger.info("Creating single-file torrent for {}...",
606606
parent.getName());
@@ -676,24 +676,24 @@ private static class CallableChunkHasher implements Callable<String> {
676676

677677
private final MessageDigest md;
678678
private final ByteBuffer data;
679-
private final ByteBufferRentalService bbrs;
679+
private final ByteBufferPool bbp;
680680

681-
CallableChunkHasher(ByteBuffer rentedBuffer, ByteBufferRentalService bbrs) throws NoSuchAlgorithmException {
681+
CallableChunkHasher(ByteBuffer rentedBuffer, ByteBufferPool bbp) throws NoSuchAlgorithmException {
682682
this.md = MessageDigest.getInstance("SHA-1");
683683

684684
rentedBuffer.mark();
685685
rentedBuffer.reset();
686686
this.data = rentedBuffer;
687687

688-
this.bbrs = bbrs;
688+
this.bbp = bbp;
689689
}
690690

691691
@Override
692692
public String call() throws UnsupportedEncodingException, InterruptedException {
693693
this.md.reset();
694694
this.md.update(this.data);
695695

696-
bbrs.put( this.data );
696+
this.bbp.getPool().returnObject(this.data);
697697

698698
return new String(md.digest(), Torrent.BYTE_ENCODING);
699699
}
@@ -715,15 +715,15 @@ public String call() throws UnsupportedEncodingException, InterruptedException {
715715
* @param file The file to hash.
716716
*/
717717
private static String hashFile(File file, int pieceLenght)
718-
throws InterruptedException, IOException, NoSuchAlgorithmException {
718+
throws InterruptedException, IOException, NoSuchAlgorithmException, Exception {
719719
return Torrent.hashFiles(Arrays.asList(new File[] { file }), pieceLenght);
720720
}
721721

722722
private static String hashFiles(List<File> files, int pieceLenght)
723-
throws InterruptedException, IOException, NoSuchAlgorithmException {
723+
throws InterruptedException, IOException, NoSuchAlgorithmException, Exception {
724724
int threads = getHashingThreadsCount();
725725
ExecutorService executor = Executors.newFixedThreadPool(threads);
726-
final ByteBufferRentalService bbrs = new ByteBufferRentalService(threads + 1, pieceLenght);
726+
final ByteBufferPool bbp = new ByteBufferPool(threads + 1, pieceLenght);
727727
List<Future<String>> results = new LinkedList<Future<String>>();
728728
StringBuilder hashes = new StringBuilder();
729729

@@ -748,13 +748,14 @@ private static String hashFiles(List<File> files, int pieceLenght)
748748
int step = 10;
749749

750750
try {
751-
buffer = bbrs.take();
751+
buffer = bbp.getPool().borrowObject();
752752

753753
while (channel.read(buffer) > 0) {
754754
if (buffer.remaining() == 0) {
755755
buffer.clear();
756-
results.add(executor.submit(new CallableChunkHasher(buffer, bbrs)));
757-
buffer = bbrs.take();
756+
results.add(executor.submit(new CallableChunkHasher(buffer, bbp)));
757+
758+
buffer = bbp.getPool().borrowObject();
758759
}
759760

760761
if (results.size() >= threads) {
@@ -776,7 +777,7 @@ private static String hashFiles(List<File> files, int pieceLenght)
776777
if ((buffer != null) && (buffer.position() > 0)) {
777778
buffer.limit(buffer.position());
778779
buffer.position(0);
779-
results.add(executor.submit(new CallableChunkHasher(buffer, bbrs)));
780+
results.add(executor.submit(new CallableChunkHasher(buffer, bbp)));
780781
}
781782

782783
pieces += accumulateHashes(hashes, results);

0 commit comments

Comments
 (0)