1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-08-13 17:03:45 -04:00

* some cleanup

This commit is contained in:
Reinhard Pointner 2008-07-11 18:55:36 +00:00
parent 25e5bde57b
commit 68dc5b61d0
2 changed files with 63 additions and 59 deletions

View File

@ -6,36 +6,47 @@ import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Torrent {
private String name;
private String encoding;
private String createdBy;
private String announce;
private String comment;
private Long creationDate;
private final String name;
private final String encoding;
private final String createdBy;
private final String announce;
private final String comment;
private final Long creationDate;
private final Long pieceLength;
private List<Entry> files;
private final List<Entry> files;
private boolean singleFileTorrent;
private final boolean singleFileTorrent;
public Torrent(File torrent) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(torrent));
Map<?, ?> torrentMap = null;
try {
torrentMap = BDecoder.decode(in);
} finally {
in.close();
}
this(new FileInputStream(torrent));
}
/**
* Load torrent data from an <code>InputStream</code>. The given stream will be closed
* after data has been read.
*/
public Torrent(InputStream inputStream) throws IOException {
this(decodeTorrent(inputStream));
}
public Torrent(Map<?, ?> torrentMap) {
Charset charset = Charset.forName("UTF-8");
@ -43,8 +54,9 @@ public class Torrent {
try {
charset = Charset.forName(encoding);
} catch (Exception e) {
} catch (IllegalArgumentException e) {
// invalid encoding, just keep using UTF-8
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.WARNING, e.getMessage());
}
createdBy = decodeString(torrentMap.get("created by"), charset);
@ -55,17 +67,19 @@ public class Torrent {
Map<?, ?> infoMap = (Map<?, ?>) torrentMap.get("info");
name = decodeString(infoMap.get("name"), charset);
pieceLength = (Long) infoMap.get("piece length");
if (infoMap.containsKey("files")) {
// torrent contains multiple entries
singleFileTorrent = false;
files = new ArrayList<Entry>();
List<Entry> entries = new ArrayList<Entry>();
for (Object fileMapObject : (List<?>) infoMap.get("files")) {
Map<?, ?> fileMap = (Map<?, ?>) fileMapObject;
List<?> pathList = (List<?>) fileMap.get("path");
StringBuilder pathBuffer = new StringBuilder();
StringBuilder pathBuilder = new StringBuilder();
String entryName = null;
Iterator<?> iterator = pathList.iterator();
@ -74,8 +88,8 @@ public class Torrent {
String pathElement = decodeString(iterator.next(), charset);
if (iterator.hasNext()) {
pathBuffer.append(pathElement);
pathBuffer.append("/");
pathBuilder.append(pathElement);
pathBuilder.append("/");
} else {
// the last element in the path list is the filename
entryName = pathElement;
@ -84,25 +98,26 @@ public class Torrent {
Long length = decodeLong(fileMap.get("length"));
files.add(new Entry(entryName, length, pathBuffer.toString()));
entries.add(new Entry(entryName, length, pathBuilder.toString()));
}
files = Collections.unmodifiableList(entries);
} else {
// single file torrent
singleFileTorrent = true;
files = new ArrayList<Entry>(1);
Long length = decodeLong(infoMap.get("length"));
files.add(new Entry(name, length, ""));
files = Collections.singletonList(new Entry(name, length, ""));
}
}
private String decodeString(Object bytearray, Charset charset) {
if (bytearray == null)
private String decodeString(Object byteArray, Charset charset) {
if (byteArray == null)
return null;
return new String((byte[]) bytearray, charset);
return new String((byte[]) byteArray, charset);
}
@ -149,19 +164,35 @@ public class Torrent {
}
public Long getPieceLength() {
return pieceLength;
}
public boolean isSingleFileTorrent() {
return singleFileTorrent;
}
private static Map<?, ?> decodeTorrent(InputStream torrent) throws IOException {
BufferedInputStream in = new BufferedInputStream(torrent);
try {
return BDecoder.decode(in);
} finally {
in.close();
}
}
public static class Entry {
private String name;
private long length;
private String path;
private final String name;
private final Long length;
private final String path;
public Entry(String name, Long length, String path) {
public Entry(String name, long length, String path) {
this.name = name;
this.length = length;
this.path = path;

View File

@ -1,27 +0,0 @@
package net.sourceforge.filebot.ui.transferablepolicies;
import java.awt.datatransfer.Transferable;
public class NullTransferablePolicy implements TransferablePolicy {
@Override
public boolean accept(Transferable tr) {
return false;
}
@Override
public void handleTransferable(Transferable tr, boolean add) {
}
@Override
public String getDescription() {
return null;
}
}