}
Map<String, BEValue> torrent = new HashMap<String, BEValue>();
if (announce != null) {
torrent.put("announce", new BEValue(announce.toString()));
}
if (announceList != null) {
List<BEValue> tiers = new LinkedList<BEValue>();
for (List<URI> trackers : announceList) {
List<BEValue> tierInfo = new LinkedList<BEValue>();
for (URI trackerURI : trackers) {
tierInfo.add(new BEValue(trackerURI.toString()));
}
tiers.add(new BEValue(tierInfo));
}
torrent.put("announce-list", new BEValue(tiers));
}
torrent.put("creation date", new BEValue(new Date().getTime() / 1000));
torrent.put("created by", new BEValue(createdBy));
Map<String, BEValue> info = new TreeMap<String, BEValue>();
info.put("name", new BEValue(parent.getName()));
info.put("piece length", new BEValue(Torrent.PIECE_LENGTH));
if (files == null || files.isEmpty()) {
info.put("length", new BEValue(parent.length()));
info.put("pieces", new BEValue(Torrent.hashFile(parent),
Torrent.BYTE_ENCODING));
} else {
List<BEValue> fileInfo = new LinkedList<BEValue>();
for (File file : files) {
Map<String, BEValue> fileMap = new HashMap<String, BEValue>();
fileMap.put("length", new BEValue(file.length()));
LinkedList<BEValue> filePath = new LinkedList<BEValue>();
while (file != null) {
if (file.equals(parent)) {
break;
}
filePath.addFirst(new BEValue(file.getName()));
file = file.getParentFile();
}
fileMap.put("path", new BEValue(filePath));
fileInfo.add(new BEValue(fileMap));
}
info.put("files", new BEValue(fileInfo));
info.put("pieces", new BEValue(Torrent.hashFiles(files),
Torrent.BYTE_ENCODING));
}
torrent.put("info", new BEValue(info));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BEncoder.bencode(new BEValue(torrent), baos);
return new Torrent(baos.toByteArray(), true);
}