Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 13 additions & 12 deletions src/main/java/com/turn/ttorrent/common/Torrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public TorrentFile(File file, long size) {
* BitTorrent specification) and create a Torrent object from it.
*
* @param torrent The meta-info byte data.
* @param parent The parent directory or location of the torrent files.
* @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.
Expand Down Expand Up @@ -401,22 +402,22 @@ public boolean isSeeder() {
return this.seeder;
}

/**
/**
* Save this torrent meta-info structure into a .torrent file.
*
* @param file The file to write to.
* @throws IOException If an I/O error occurs while writing the file.
*/
public void save(File file) throws IOException {
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
fOut.write(this.getEncoded());
} finally {
if (fOut != null){
fOut.close();
}
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
fOut.write(this.getEncoded());
} finally {
if (fOut != null) {
fOut.close();
}
}
}

public static byte[] hash(byte[] data) throws NoSuchAlgorithmException {
Expand Down Expand Up @@ -961,8 +962,8 @@ public static void main(String[] args) {
torrent = Torrent.create(source, announceURI, creator);
}

fos.write(torrent.getEncoded());
} else {
fos.write(torrent.getEncoded());
} else {
Torrent.load(new File(filenameValue), true);
}
} catch (Exception e) {
Expand Down
33 changes: 17 additions & 16 deletions src/main/java/com/turn/ttorrent/tracker/Tracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,15 @@ public void stop() {
}
}

public ConcurrentMap<String, TrackedTorrent> getTorrentsMap() {
return torrents;
}

public Collection<TrackedTorrent> getTrackedTorrents(){
return torrents.values();
}
/**
*
* @return list of tracker's torrents
*/
public Collection<TrackedTorrent> getTrackedTorrents() {
return torrents.values();
}

/**
/**
* Announce a new torrent on this tracker.
*
* <p>
Expand All @@ -206,7 +206,8 @@ public Collection<TrackedTorrent> getTrackedTorrents(){
* different from the supplied Torrent object if the tracker already
* contained a torrent with the same hash.
*/
public synchronized TrackedTorrent announce(Torrent torrent) throws IOException, NoSuchAlgorithmException {
public synchronized TrackedTorrent announce(Torrent torrent)
throws IOException, NoSuchAlgorithmException {
TrackedTorrent existing = this.torrents.get(torrent.getHexInfoHash());

if (existing != null) {
Expand All @@ -215,13 +216,13 @@ public synchronized TrackedTorrent announce(Torrent torrent) throws IOException,
return existing;
}

final TrackedTorrent result;
if (torrent instanceof TrackedTorrent) {
result = (TrackedTorrent) torrent;
} else {
result = new TrackedTorrent(torrent);
}
this.torrents.put(torrent.getHexInfoHash(), result);
final TrackedTorrent result;
if (torrent instanceof TrackedTorrent) {
result = (TrackedTorrent)torrent;
} else {
result = new TrackedTorrent(torrent);
}
this.torrents.put(torrent.getHexInfoHash(), result);
logger.info("Registered new torrent for '{}' with hash {}.",
torrent.getName(), torrent.getHexInfoHash());
return result;
Expand Down
84 changes: 42 additions & 42 deletions src/test/java/com/turn/ttorrent/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,50 @@

public class FileUtil {

public static File getTempDirectory() {
return new File(System.getProperty("java.io.tmpdir"));
}
public static File getTempDirectory() {
return new File(System.getProperty("java.io.tmpdir"));
}

public static void delete(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f: files) {
delete(f);
}
}
deleteFile(file);
}
public static void delete(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
delete(f);
}
}
deleteFile(file);
}

private static boolean deleteFile(File file) {
if (!file.exists()) return false;
for (int i=0; i<10; i++) {
if (file.delete()) return true;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
//
}
}
return false;
}
private static boolean deleteFile(File file) {
if (!file.exists()) return false;
for (int i = 0; i < 10; i++) {
if (file.delete()) return true;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
//
}
}
return false;
}

public static void writeFile(File file, String content) throws IOException {
FileWriter fw = null;
try {
fw = new FileWriter(file);
fw.write(content);
} finally {
close(fw);
}
}
public static void writeFile(File file, String content) throws IOException {
FileWriter fw = null;
try {
fw = new FileWriter(file);
fw.write(content);
} finally {
close(fw);
}
}

public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
//
}
}
}
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
//
}
}
}
}
Loading