栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Java中生成.torrent?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Java中生成.torrent?

我整理了这段独立的Java代码,以准备一个带有单个文件的.torrent文件。

通过调用

createTorrent()
.torrent文件的名称,共享文件的名称和跟踪器URL 来创建.torrent文件。

createTorrent()
用于使用Java的类
hashPieces()
对文件 片段
进行哈希处理
MessageDigest
。然后
createTorrent()
准备一个包含洪流元数据的 元信息字典 。然后,使用这些方法以适当的
Benpre 格式序列化该词典,
enpre*()
并将其保存在.torrent文件中。

有关详细信息,请参见BitTorrent规范。

public class Torrent {    private static void enpreObject(Object o, OutputStream out) throws IOException {        if (o instanceof String) enpreString((String)o, out);        else if (o instanceof Map) enpreMap((Map)o, out);        else if (o instanceof byte[]) enpreBytes((byte[])o, out);        else if (o instanceof Number) enpreLong(((Number) o).longValue(), out);        else throw new Error("Unencodable type");    }    private static void enpreLong(long value, OutputStream out) throws IOException {        out.write('i');        out.write(Long.toString(value).getBytes("US-ASCII"));        out.write('e');    }    private static void enpreBytes(byte[] bytes, OutputStream out) throws IOException {        out.write(Integer.toString(bytes.length).getBytes("US-ASCII"));        out.write(':');        out.write(bytes);    }    private static void enpreString(String str, OutputStream out) throws IOException {        enpreBytes(str.getBytes("UTF-8"), out);    }    private static void enpreMap(Map<String,Object> map, OutputStream out) throws IOException{        // Sort the map. A generic enprer should sort by key bytes        SortedMap<String,Object> sortedMap = new TreeMap<String, Object>(map);        out.write('d');        for (Entry<String, Object> e : sortedMap.entrySet()) { enpreString(e.getKey(), out); enpreObject(e.getValue(), out);        }        out.write('e');    }    private static byte[] hashPieces(File file, int pieceLength) throws IOException {        MessageDigest sha1;        try { sha1 = MessageDigest.getInstance("SHA");        } catch (NoSuchAlgorithmException e) { throw new Error("SHA1 not supported");        }        InputStream in = new FileInputStream(file);        ByteArrayOutputStream pieces = new ByteArrayOutputStream();        byte[] bytes = new byte[pieceLength];        int pieceByteCount  = 0, readCount = in.read(bytes, 0, pieceLength);        while (readCount != -1) { pieceByteCount += readCount; sha1.update(bytes, 0, readCount); if (pieceByteCount == pieceLength) {     pieceByteCount = 0;     pieces.write(sha1.digest()); } readCount = in.read(bytes, 0, pieceLength-pieceByteCount);        }        in.close();        if (pieceByteCount > 0) pieces.write(sha1.digest());        return pieces.toByteArray();    }    public static void createTorrent(File file, File sharedFile, String announceURL) throws IOException {        final int pieceLength = 512*1024;        Map<String,Object> info = new HashMap<String,Object>();        info.put("name", sharedFile.getName());        info.put("length", sharedFile.length());        info.put("piece length", pieceLength);        info.put("pieces", hashPieces(sharedFile, pieceLength));        Map<String,Object> metainfo = new HashMap<String,Object>();        metainfo.put("announce", announceURL);        metainfo.put("info", info);        OutputStream out = new FileOutputStream(file);        enpreMap(metainfo, out);        out.close();    }    public static void main(String[] args) throws Exception {        createTorrent(new File("C:/x.torrent"), new File("C:/file"), "http://example.com/announce");    }}

代码编辑: 使其更紧凑,更清晰地查看方法,在适当的地方使用字符文字,使用

instanceof Number
。最近 ,我使用块I /
O读取了文件,
因为我试图将其用于实数,而字节I / O只是速度较慢,



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508410.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号