Skip to content

Commit

Permalink
Merge branch 'findbugs-fixes'
Browse files Browse the repository at this point in the history
Merge a series of fixes resulting from a FindBugs run analysis.

Signed-off-by: Maxime Petazzoni <[email protected]>
  • Loading branch information
Maxime Petazzoni committed Jan 9, 2012
2 parents 21712ec + aef7ee9 commit a1d673f
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 108 deletions.
3 changes: 2 additions & 1 deletion INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ tracker (an HTTP service), and a BitTorrent client. The only dependencies of
the BitTorrent library are:

* the log4j library
* the slf4j logging library
* the SimpleHTTPFramework

These two libraries are provided in the lib/ directory, and are automatically
These libraries are provided in the lib/ directory, and are automatically
included in the JAR file created by the build process.


Expand Down
3 changes: 2 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Authors
* Thomas Zink <[email protected]>
- Fixed a piece length computation issue when the total torrent size is an
exact multiple of the piece size.

* Johan Parent <[email protected]>
- Fixed a bug in unfresh peer collection

Caveats
-------
Expand Down
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<property name="src.dir" location="src" />

<!-- Release version. -->
<property name="project.version" value="1.1" />
<property name="project.version" value="1.1.1" />

<property name="jar.location"
value="${dist.dir}/${ant.project.name}-${project.version}.jar" />
Expand Down
Binary file added dist/ttorrent-1.1.1.jar
Binary file not shown.
8 changes: 4 additions & 4 deletions src/com/turn/ttorrent/client/Announce.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ private Map<String, BEValue> announce(AnnounceEvent event,
}

params.put("peer_id", this.id);
params.put("port", new Integer(this.address.getPort()).toString());
params.put("uploaded", new Long(this.torrent.getUploaded()).toString());
params.put("downloaded", new Long(this.torrent.getDownloaded()).toString());
params.put("left", new Long(this.torrent.getLeft()).toString());
params.put("port", Integer.valueOf(this.address.getPort()).toString());
params.put("uploaded", Long.valueOf(this.torrent.getUploaded()).toString());
params.put("downloaded", Long.valueOf(this.torrent.getDownloaded()).toString());
params.put("left", Long.valueOf(this.torrent.getLeft()).toString());

if (!AnnounceEvent.NONE.equals(event)) {
params.put("event", event.name().toLowerCase());
Expand Down
14 changes: 10 additions & 4 deletions src/com/turn/ttorrent/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public synchronized void info() {
}

logger.info("BitTorrent client {}, {}/{}/{} peers, {}/{}/{} pieces " +
"({}%), {}/{} kB/s.",
"({}%, {} requested), {}/{} kB/s.",
new Object[] {
this.getState().name(),
choked,
Expand All @@ -384,6 +384,7 @@ public synchronized void info() {
this.torrent.getAvailablePieces().cardinality(),
this.torrent.getPieceCount(),
String.format("%.2f", this.torrent.getCompletion()),
this.torrent.getRequestedPieces().cardinality(),
String.format("%.2f", dl/1024.0),
String.format("%.2f", ul/1024.0)
});
Expand Down Expand Up @@ -437,14 +438,19 @@ private SharingPeer getOrCreatePeer(byte[] peerId, String ip, int port) {
// Set peer ID for perviously known peer.
peer.setPeerId(search.getPeerId());

// Replace the mapping for this peer from its host
// identifier to its now known peer ID.
this.peers.remove(peer.getHostIdentifier());
this.peers.putIfAbsent(peer.getHexPeerId(), peer);
this.peers.put(peer.getHexPeerId(), peer);
return peer;
}
}

// Last case, it really didn't exist already, add it, either from
// peer ID or host identifier, whatever we have so that we can find
// it later.
peer = new SharingPeer(ip, port, search.getPeerId(), this.torrent);
this.peers.putIfAbsent(peer.hasPeerId()
this.peers.put(peer.hasPeerId()
? peer.getHexPeerId()
: peer.getHostIdentifier(),
peer);
Expand Down Expand Up @@ -849,7 +855,7 @@ private synchronized void seed() {
*
* @author mpetazzoni
*/
private class StopSeedingTask extends TimerTask {
private static class StopSeedingTask extends TimerTask {

private Client client;

Expand Down
2 changes: 1 addition & 1 deletion src/com/turn/ttorrent/client/Handshake.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public byte[] getPeerId() {

public static Handshake parse(ByteBuffer buffer)
throws ParseException, UnsupportedEncodingException {
int pstrlen = new Byte(buffer.get()).intValue();
int pstrlen = Byte.valueOf(buffer.get()).intValue();
if (pstrlen < 0 ||
buffer.remaining() != BASE_HANDSHAKE_LENGTH + pstrlen - 1) {
throw new ParseException("Incorrect handshake message length " +
Expand Down
54 changes: 32 additions & 22 deletions src/com/turn/ttorrent/client/SharedTorrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
Expand Down Expand Up @@ -94,14 +96,13 @@ public class SharedTorrent extends Torrent implements PeerActivityListener {
* @param torrent The Torrent object.
* @param destDir The destination directory or location of the torrent
* files.
* @throws IllegalArgumentException When the info dictionnary can't be
* encoded and hashed back to create the torrent's SHA-1 hash.
* @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 accessed.
* @throws IOException If the torrent file cannot be read or decoded.
* @throws NoSuchAlgorithmException
*/
public SharedTorrent(Torrent torrent, File destDir)
throws IllegalArgumentException, FileNotFoundException, IOException {
throws FileNotFoundException, IOException, NoSuchAlgorithmException {
this(torrent, destDir, false);
}

Expand All @@ -115,14 +116,13 @@ public SharedTorrent(Torrent torrent, File destDir)
* files.
* @param seeder Whether we're a seeder for this torrent or not (disables
* validation).
* @throws IllegalArgumentException When the info dictionnary can't be
* encoded and hashed back to create the torrent's SHA-1 hash.
* @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 accessed.
* @throws IOException If the torrent file cannot be read or decoded.
* @throws NoSuchAlgorithmException
*/
public SharedTorrent(Torrent torrent, File destDir, boolean seeder)
throws IllegalArgumentException, FileNotFoundException, IOException {
throws FileNotFoundException, IOException, NoSuchAlgorithmException {
this(torrent.getEncoded(), destDir, seeder);
}

Expand All @@ -131,14 +131,12 @@ public SharedTorrent(Torrent torrent, File destDir, boolean seeder)
* @param torrent The metainfo byte data.
* @param destDir The destination directory or location of the torrent
* files.
* @throws IllegalArgumentException When the info dictionary can't be
* encoded and hashed back to create the torrent's SHA-1 hash.
* @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 accessed.
* @throws IOException If the torrent file cannot be read or decoded.
*/
public SharedTorrent(byte[] torrent, File destDir)
throws IllegalArgumentException, FileNotFoundException, IOException {
throws FileNotFoundException, IOException, NoSuchAlgorithmException {
this(torrent, destDir, false);
}

Expand All @@ -148,14 +146,13 @@ public SharedTorrent(byte[] torrent, File destDir)
* @param parent The parent directory or location the torrent files.
* @param seeder Whether we're a seeder for this torrent or not (disables
* validation).
* @throws IllegalArgumentException When the info dictionary can't be
* encoded and hashed back to create the torrent's SHA-1 hash.
* @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 accessed.
* @throws IOException If the torrent file cannot be read or decoded.
* @throws NoSuchAlgorithmException
*/
public SharedTorrent(byte[] torrent, File parent, boolean seeder)
throws IllegalArgumentException, FileNotFoundException, IOException {
throws FileNotFoundException, IOException, NoSuchAlgorithmException {
super(torrent, parent, seeder);

if (parent == null || !parent.isDirectory()) {
Expand Down Expand Up @@ -214,12 +211,11 @@ public SharedTorrent(byte[] torrent, File parent, boolean seeder)
* @param source The <code>.torrent</code> file to read the torrent
* meta-info from.
* @param parent The parent directory or location of the torrent files.
* @throws IllegalArgumentException When the info dictionnary can't be
* encoded and hashed back to create the torrent's SHA-1 hash.
* @throws IOException When the torrent file cannot be read.
* @throws IOException When the torrent file cannot be read or decoded.
* @throws NoSuchAlgorithmException
*/
public static SharedTorrent fromFile(File source, File parent)
throws IllegalArgumentException, IOException {
throws IOException, NoSuchAlgorithmException {
FileInputStream fis = new FileInputStream(source);
byte[] data = new byte[(int)source.length()];
fis.read(data);
Expand Down Expand Up @@ -278,8 +274,8 @@ public synchronized void init() throws InterruptedException, IOException {
throw new IllegalStateException("Torrent was already initialized!");
}

int nPieces = new Double(Math.ceil((double)this.getSize() /
this.pieceLength)).intValue();
int nPieces = (int) (Math.ceil(
(double)this.getSize() / this.pieceLength));
this.pieces = new Piece[nPieces];
this.completedPieces = new BitSet(nPieces);

Expand Down Expand Up @@ -404,6 +400,8 @@ public BitSet getAvailablePieces() {
return availablePieces;
}

/** Return a copy of the completed pieces bitset.
*/
public BitSet getCompletedPieces() {
if (!this.isInitialized()) {
throw new IllegalStateException("Torrent not yet initialized!");
Expand All @@ -414,6 +412,18 @@ public BitSet getCompletedPieces() {
}
}

/** Return a copy of the requested pieces bitset.
*/
public BitSet getRequestedPieces() {
if (!this.isInitialized()) {
throw new IllegalStateException("Torrent not yet initialized!");
}

synchronized (this.requestedPieces) {
return (BitSet)this.requestedPieces.clone();
}
}

/** Tells whether this torrent has been fully downloaded, or is fully
* available locally.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/com/turn/ttorrent/client/peer/SharingPeer.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public synchronized void downloadPiece(Piece piece)
IllegalStateException up = new IllegalStateException(
"Trying to download a piece while previous " +
"download not completed!");
logger.warn("{}", up);
logger.warn("What's going on? {}", up.getMessage(), up);
throw up; // ah ah.
}

Expand Down Expand Up @@ -403,6 +403,8 @@ private synchronized Set<Message.RequestMessage> cancelPendingRequests() {
request.getOffset(), request.getLength()));
requests.add(request);
}

this.requests = null;
}

return requests;
Expand Down
Loading

0 comments on commit a1d673f

Please sign in to comment.