Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow for null directory to be passed to SharedTorrent.fromFile() constr... #103

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
82fb651
Fix read when message size wasn't read in one step
pankdm Apr 10, 2013
728edf8
Throw AnnounceException when current tracker client isn't available.
sroze Jul 18, 2013
9370d86
Fix order of uploaded/downloaded parameters in HTTPAnnounceRequestMes…
mpetazzoni Jul 22, 2013
1ad97ef
Merge pull request #45 from sroze/trackerless-patch
mpetazzoni Jul 22, 2013
2e643f4
add the ability to set upload/download rate-limits (in kB/sec.) on sh…
ptgoetz Jul 19, 2013
b15ea5e
add javadoc documentation for upload/download rate-limit functionality
ptgoetz Jul 22, 2013
73b78f2
reduce rate limit code duplication by refactoring code present in bot…
ptgoetz Jul 22, 2013
3ef9e9b
add example of upload/download rate limiting to the client usage sample.
ptgoetz Jul 22, 2013
13db7ab
add comments documenting the drawbacks/potential improvements for the…
ptgoetz Jul 22, 2013
00bc8f2
minor formatting/style modifications based on feedback from pull requ…
ptgoetz Jul 22, 2013
42775d5
Merge pull request #49 from ptgoetz/rate-limits
mpetazzoni Jul 23, 2013
59a2ec1
Fix a few styling issues from recent contributions
mpetazzoni Jul 23, 2013
c004fa8
Merge pull request #40 from pankdm/fix-incomplete-read
mpetazzoni Jul 23, 2013
64b8865
fixes Issue #47
oxlade39 Jul 22, 2013
a47a275
fixes turn/ttorrent#51
pwoodworth Jul 24, 2013
0dcb033
Merge pull request #50 from oxlade39/bug_fix
mpetazzoni Jul 24, 2013
bf4d679
Merge pull request #53 from pwoodworth/issue-51
mpetazzoni Jul 25, 2013
c1dbcd9
Update pom for compatibility with the maven-release-plugin
pwoodworth Jul 30, 2013
9e8abed
Use maven-shade-plugin to produce executable jar
pwoodworth Jul 30, 2013
5a499ce
Clean up javadoc problems
pwoodworth Jul 30, 2013
c06b110
Merge pull request #54 from pwoodworth/shaded
mpetazzoni Jul 31, 2013
f69190b
Merge pull request #55 from pwoodworth/releaseplugin
mpetazzoni Jul 31, 2013
89a6754
Merge pull request #56 from pwoodworth/javadoc
mpetazzoni Jul 31, 2013
0080296
Fix pom parent relative path
pwoodworth Aug 14, 2013
9e09ee5
Adding more platform agnostic shell scripts
pwoodworth Aug 15, 2013
d50eb99
Removing old shell scripts
pwoodworth Aug 15, 2013
86f4a2f
Merge pull request #57 from pwoodworth/scripts
mpetazzoni Aug 16, 2013
1c8dbfc
Move entry-point main methods to separate package
pwoodworth Aug 26, 2013
d33c361
Merge pull request #59 from pwoodworth/climove
mpetazzoni Aug 26, 2013
dbfd004
Fixes turn/ttorrent#42
pwoodworth Aug 28, 2013
06a3dfc
Merge pull request #60 from pwoodworth/issue-42
mpetazzoni Aug 28, 2013
dd8e7c9
Use commons-io throughout
pwoodworth Sep 11, 2013
f72705f
Merge pull request #63 from pwoodworth/commonsio
mpetazzoni Sep 12, 2013
d243274
Use commons-codec for hex and digest encoding
pwoodworth Aug 28, 2013
6fd448f
Merge pull request #64 from pwoodworth/codec
mpetazzoni Sep 17, 2013
869f664
Fix Piece compareTo ordering
pwoodworth Sep 20, 2013
32c66e1
Correcting improper use of Java 7 API
pwoodworth Sep 26, 2013
d787d9c
Merge pull request #65 from pwoodworth/piececompare
mpetazzoni Sep 30, 2013
cce0c02
Developer contact info updates
mpetazzoni Oct 24, 2013
01276bb
allow for null directory to be passed to SharedTorrent.fromFile() con…
Sep 5, 2014
d815e16
don't initialize if already done
Sep 5, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -78,6 +78,12 @@
</repositories>

<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.8</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
15 changes: 4 additions & 11 deletions src/main/java/com/turn/ttorrent/client/Piece.java
Original file line number Diff line number Diff line change
@@ -21,7 +21,6 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.concurrent.Callable;

@@ -159,16 +158,10 @@ public synchronized boolean validate() throws IOException {
logger.trace("Validating {}...", this);
this.valid = false;

try {
// TODO: remove cast to int when large ByteBuffer support is
// implemented in Java.
ByteBuffer buffer = this._read(0, this.length);
byte[] data = new byte[(int)this.length];
buffer.get(data);
this.valid = Arrays.equals(Torrent.hash(data), this.hash);
} catch (NoSuchAlgorithmException nsae) {
logger.error("{}", nsae);
}
ByteBuffer buffer = this._read(0, this.length);
byte[] data = new byte[(int)this.length];
buffer.get(data);
this.valid = Arrays.equals(Torrent.hash(data), this.hash);

return this.isValid();
}
15 changes: 5 additions & 10 deletions src/main/java/com/turn/ttorrent/client/SharedTorrent.java
Original file line number Diff line number Diff line change
@@ -29,7 +29,6 @@
import java.io.IOException;
import java.nio.ByteBuffer;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
@@ -117,10 +116,9 @@ public class SharedTorrent extends Torrent implements PeerActivityListener {
* @throws FileNotFoundException If the torrent file location or
* destination directory does not exist and can't be created.
* @throws IOException If the torrent file cannot be read or decoded.
* @throws NoSuchAlgorithmException
*/
public SharedTorrent(Torrent torrent, File destDir)
throws FileNotFoundException, IOException, NoSuchAlgorithmException {
throws FileNotFoundException, IOException {
this(torrent, destDir, false);
}

@@ -140,10 +138,9 @@ public SharedTorrent(Torrent torrent, File destDir)
* @throws FileNotFoundException If the torrent file location or
* destination directory does not exist and can't be created.
* @throws IOException If the torrent file cannot be read or decoded.
* @throws NoSuchAlgorithmException
*/
public SharedTorrent(Torrent torrent, File destDir, boolean seeder)
throws FileNotFoundException, IOException, NoSuchAlgorithmException {
throws FileNotFoundException, IOException {
this(torrent.getEncoded(), destDir, seeder);
}

@@ -158,7 +155,7 @@ public SharedTorrent(Torrent torrent, File destDir, boolean seeder)
* @throws IOException If the torrent file cannot be read or decoded.
*/
public SharedTorrent(byte[] torrent, File destDir)
throws FileNotFoundException, IOException, NoSuchAlgorithmException {
throws FileNotFoundException, IOException {
this(torrent, destDir, false);
}

@@ -172,10 +169,9 @@ public SharedTorrent(byte[] torrent, File destDir)
* @throws FileNotFoundException If the torrent file location or
* destination directory does not exist and can't be created.
* @throws IOException If the torrent file cannot be read or decoded.
* @throws NoSuchAlgorithmException
*/
public SharedTorrent(byte[] torrent, File parent, boolean seeder)
throws FileNotFoundException, IOException, NoSuchAlgorithmException {
throws FileNotFoundException, IOException {
super(torrent, seeder);

if (parent == null || !parent.isDirectory()) {
@@ -236,10 +232,9 @@ public SharedTorrent(byte[] torrent, File parent, boolean seeder)
* meta-info from.
* @param parent The parent directory or location of the torrent files.
* @throws IOException When the torrent file cannot be read or decoded.
* @throws NoSuchAlgorithmException
*/
public static SharedTorrent fromFile(File source, File parent)
throws IOException, NoSuchAlgorithmException {
throws IOException {
byte[] data = FileUtils.readFileToByteArray(source);
return new SharedTorrent(data, parent);
}
46 changes: 17 additions & 29 deletions src/main/java/com/turn/ttorrent/common/Torrent.java
Original file line number Diff line number Diff line change
@@ -26,13 +26,11 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
@@ -49,6 +47,8 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -129,11 +129,8 @@ public TorrentFile(File file, long size) {
* @param seeder Whether we'll be seeding for this torrent or not.
* @throws IOException When the info dictionary can't be read or
* encoded and hashed back to create the torrent's SHA-1 hash.
* @throws NoSuchAlgorithmException If the SHA-1 algorithm is not
* available.
*/
public Torrent(byte[] torrent, boolean seeder)
throws IOException, NoSuchAlgorithmException {
public Torrent(byte[] torrent, boolean seeder) throws IOException {
this.encoded = torrent;
this.seeder = seeder;

@@ -404,10 +401,8 @@ public void save(OutputStream output) throws IOException {
output.write(this.getEncoded());
}

public static byte[] hash(byte[] data) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(data);
return md.digest();
public static byte[] hash(byte[] data) {
return DigestUtils.sha1(data);
}

/**
@@ -417,8 +412,7 @@ public static byte[] hash(byte[] data) throws NoSuchAlgorithmException {
* @param bytes The byte array to convert.
*/
public static String byteArrayToHexString(byte[] bytes) {
BigInteger bi = new BigInteger(1, bytes);
return String.format("%0" + (bytes.length << 1) + "X", bi);
return new String(Hex.encodeHex(bytes, false));
}

/**
@@ -477,10 +471,8 @@ protected static int getHashingThreadsCount() {
* @param torrent The abstract {@link File} object representing the
* <tt>.torrent</tt> file to load.
* @throws IOException When the torrent file cannot be read.
* @throws NoSuchAlgorithmException
*/
public static Torrent load(File torrent)
throws IOException, NoSuchAlgorithmException {
public static Torrent load(File torrent) throws IOException {
return Torrent.load(torrent, false);
}

@@ -492,10 +484,9 @@ public static Torrent load(File torrent)
* @param seeder Whether we are a seeder for this torrent or not (disables
* local data validation).
* @throws IOException When the torrent file cannot be read.
* @throws NoSuchAlgorithmException
*/
public static Torrent load(File torrent, boolean seeder)
throws IOException, NoSuchAlgorithmException {
throws IOException {
byte[] data = FileUtils.readFileToByteArray(torrent);
return new Torrent(data, seeder);
}
@@ -517,7 +508,7 @@ public static Torrent load(File torrent, boolean seeder)
* torrent's creator.
*/
public static Torrent create(File source, URI announce, String createdBy)
throws NoSuchAlgorithmException, InterruptedException, IOException {
throws InterruptedException, IOException {
return Torrent.create(source, null, announce, null, createdBy);
}

@@ -539,8 +530,7 @@ public static Torrent create(File source, URI announce, String createdBy)
* torrent's creator.
*/
public static Torrent create(File parent, List<File> files, URI announce,
String createdBy) throws NoSuchAlgorithmException,
InterruptedException, IOException {
String createdBy) throws InterruptedException, IOException {
return Torrent.create(parent, files, announce, null, createdBy);
}

@@ -560,8 +550,7 @@ public static Torrent create(File parent, List<File> files, URI announce,
* torrent's creator.
*/
public static Torrent create(File source, List<List<URI>> announceList,
String createdBy) throws NoSuchAlgorithmException,
InterruptedException, IOException {
String createdBy) throws InterruptedException, IOException {
return Torrent.create(source, null, null, announceList, createdBy);
}

@@ -585,7 +574,7 @@ public static Torrent create(File source, List<List<URI>> announceList,
*/
public static Torrent create(File source, List<File> files,
List<List<URI>> announceList, String createdBy)
throws NoSuchAlgorithmException, InterruptedException, IOException {
throws InterruptedException, IOException {
return Torrent.create(source, files, null, announceList, createdBy);
}

@@ -610,7 +599,7 @@ public static Torrent create(File source, List<File> files,
*/
private static Torrent create(File parent, List<File> files, URI announce,
List<List<URI>> announceList, String createdBy)
throws NoSuchAlgorithmException, InterruptedException, IOException {
throws InterruptedException, IOException {
if (files == null || files.isEmpty()) {
logger.info("Creating single-file torrent for {}...",
parent.getName());
@@ -687,9 +676,8 @@ private static class CallableChunkHasher implements Callable<String> {
private final MessageDigest md;
private final ByteBuffer data;

CallableChunkHasher(ByteBuffer buffer)
throws NoSuchAlgorithmException {
this.md = MessageDigest.getInstance("SHA-1");
CallableChunkHasher(ByteBuffer buffer) {
this.md = DigestUtils.getSha1Digest();

this.data = ByteBuffer.allocate(buffer.remaining());
buffer.mark();
@@ -722,12 +710,12 @@ public String call() throws UnsupportedEncodingException {
* @param file The file to hash.
*/
private static String hashFile(File file)
throws NoSuchAlgorithmException, InterruptedException, IOException {
throws InterruptedException, IOException {
return Torrent.hashFiles(Arrays.asList(new File[] { file }));
}

private static String hashFiles(List<File> files)
throws NoSuchAlgorithmException, InterruptedException, IOException {
throws InterruptedException, IOException {
int threads = getHashingThreadsCount();
ExecutorService executor = Executors.newFixedThreadPool(threads);
ByteBuffer buffer = ByteBuffer.allocate(Torrent.PIECE_LENGTH);
13 changes: 3 additions & 10 deletions src/main/java/com/turn/ttorrent/tracker/TrackedTorrent.java
Original file line number Diff line number Diff line change
@@ -24,7 +24,6 @@
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@@ -77,20 +76,16 @@ public class TrackedTorrent extends Torrent {
* @param torrent The meta-info byte data.
* @throws IOException When the info dictionary can't be
* encoded and hashed back to create the torrent's SHA-1 hash.
* @throws NoSuchAlgorithmException If the SHA-1 algorithm is not
* available.
*/
public TrackedTorrent(byte[] torrent)
throws IOException, NoSuchAlgorithmException {
public TrackedTorrent(byte[] torrent) throws IOException {
super(torrent, false);

this.peers = new ConcurrentHashMap<String, TrackedPeer>();
this.answerPeers = TrackedTorrent.DEFAULT_ANSWER_NUM_PEERS;
this.announceInterval = TrackedTorrent.DEFAULT_ANNOUNCE_INTERVAL_SECONDS;
}

public TrackedTorrent(Torrent torrent)
throws IOException, NoSuchAlgorithmException {
public TrackedTorrent(Torrent torrent) throws IOException {
this(torrent.getEncoded());
}

@@ -293,10 +288,8 @@ public List<Peer> getSomePeers(TrackedPeer peer) {
* @param torrent The abstract {@link File} object representing the
* <tt>.torrent</tt> file to load.
* @throws IOException When the torrent file cannot be read.
* @throws NoSuchAlgorithmException
*/
public static TrackedTorrent load(File torrent) throws IOException,
NoSuchAlgorithmException {
public static TrackedTorrent load(File torrent) throws IOException {
byte[] data = FileUtils.readFileToByteArray(torrent);
return new TrackedTorrent(data);
}