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