Java Torrent管理:UDP协议出错
我正在尝试使用JLIBtorrent库在java中开发torrent manager,但在执行该程序时,我得到以下错误: 本编码例外: 未知协议:udp 位于com.torrent.util.TorrentInfo(TorrentInfo.java:160) 在圣主岛(圣爪哇岛:70) 该程序的参数是一个从互联网下载的洪流,它也在传输工作 “给我错误消息”功能的代码为:Java Torrent管理:UDP协议出错,java,bittorrent,torrent,Java,Bittorrent,Torrent,我正在尝试使用JLIBtorrent库在java中开发torrent manager,但在执行该程序时,我得到以下错误: 本编码例外: 未知协议:udp 位于com.torrent.util.TorrentInfo(TorrentInfo.java:160) 在圣主岛(圣爪哇岛:70) 该程序的参数是一个从互联网下载的洪流,它也在传输工作 “给我错误消息”功能的代码为: @SuppressWarnings("unchecked") public TorrentInfo(byte[] torren
@SuppressWarnings("unchecked")
public TorrentInfo(byte[] torrent_file_bytes) throws BencodingException
{
// Make sure the input is valid
if(torrent_file_bytes == null || torrent_file_bytes.length == 0)
throw new IllegalArgumentException("Torrent file bytes must be non-null and have at least 1 byte.");
// Assign the byte array
this.torrent_file_bytes = torrent_file_bytes;
// Assign the metainfo map
this.torrent_file_map = (Map<ByteBuffer,Object>)Bencoder2.decode(torrent_file_bytes);
// Try to extract the announce URL
ByteBuffer url_buff = (ByteBuffer)this.torrent_file_map.get(TorrentInfo.KEY_ANNOUNCE);
if(url_buff == null)
throw new BencodingException("Could not retrieve anounce URL from torrent metainfo. Corrupt file?");
try {
String url_string = new String(url_buff.array(), "ASCII");
URL announce_url = new URL(url_string);
this.announce_url = announce_url;
}
catch(UnsupportedEncodingException uee)
{
throw new BencodingException(uee.getLocalizedMessage());
}
catch(MalformedURLException murle)
{
throw new BencodingException(murle.getLocalizedMessage());
}
// Try to extract the info dictionary
ByteBuffer info_bytes = Bencoder2.getInfoBytes(torrent_file_bytes);
Map<ByteBuffer,Object> info_map = (Map<ByteBuffer,Object>)this.torrent_file_map.get(TorrentInfo.KEY_INFO);
if(info_map == null)
throw new BencodingException("Could not extract info dictionary from torrent metainfo dictionary. Corrupt file?");
this.info_map = info_map;
// Try to generate the info hash value
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(info_bytes.array());
byte[] info_hash = digest.digest();
this.info_hash = ByteBuffer.wrap(info_hash);
}
catch(NoSuchAlgorithmException nsae)
{
throw new BencodingException(nsae.getLocalizedMessage());
}
// Extract the piece length from the info dictionary
Integer piece_length = (Integer)this.info_map.get(TorrentInfo.KEY_PIECE_LENGTH);
if(piece_length == null)
throw new BencodingException("Could not extract piece length from info dictionary. Corrupt file?");
this.piece_length = piece_length.intValue();
// Extract the file name from the info dictionary
ByteBuffer name_bytes = (ByteBuffer)this.info_map.get(TorrentInfo.KEY_NAME);
if(name_bytes == null)
throw new BencodingException("Could not retrieve file name from info dictionary. Corrupt file?");
try {
this.file_name = new String(name_bytes.array(),"ASCII");
}
catch(UnsupportedEncodingException uee)
{
throw new BencodingException(uee.getLocalizedMessage());
}
// Extract the file length from the info dictionary
Integer file_length = (Integer)this.info_map.get(TorrentInfo.KEY_LENGTH);
if(file_length == null)
throw new BencodingException("Could not extract file length from info dictionary. Corrupt file?");
this.file_length = file_length.intValue();
// Extract the piece hashes from the info dictionary
ByteBuffer all_hashes = (ByteBuffer)this.info_map.get(TorrentInfo.KEY_PIECES);
if(all_hashes == null)
throw new BencodingException("Could not extract piece hashes from info dictionary. Corrupt file?");
byte[] all_hashes_array = all_hashes.array();
// Verify that the length of the array is a multiple of 20 bytes (160 bits)
if(all_hashes_array.length % 20 != 0)
throw new BencodingException("Piece hashes length is not a multiple of 20. Corrupt file?");
int num_pieces = all_hashes_array.length / 20;
// Copy the values of the piece hashes into the local field
this.piece_hashes = new ByteBuffer[num_pieces];
for(int i = 0; i < num_pieces; i++)
{
byte[] temp_buff = new byte[20];
System.arraycopy(all_hashes_array,i*20,temp_buff,0,20);
this.piece_hashes[i] = ByteBuffer.wrap(temp_buff);
}
}
@SuppressWarnings(“未选中”)
公共TorrentInfo(字节[]torrent\u文件\u字节)引发BencodingException
{
//确保输入有效
if(torrent_file_bytes==null | | torrent_file_bytes.length==0)
抛出新的IllegalArgumentException(“Torrent文件字节必须为非null且至少有1个字节”);
//分配字节数组
this.torrent\u file\u bytes=torrent\u file\u bytes;
//分配metainfo映射
this.torrent\u file\u map=(map)Bencoder2.decode(torrent\u file\u字节);
//尝试提取公告URL
ByteBufferURL\u buff=(ByteBuffer)this.torrent\u file\u map.get(TorrentInfo.KEY\u annound);
if(url\u buff==null)
抛出新的BencodingException(“无法从torrent metainfo.Corrupt文件检索通知URL?”);
试一试{
字符串url\u String=新字符串(url\u buff.array(),“ASCII”);
URL announce\u URL=新URL(URL\u字符串);
this.announce\u url=announce\u url;
}
捕获(不支持的编码异常uee)
{
抛出新的BencodingException(uee.getLocalizedMessage());
}
捕获(格式错误)
{
抛出新的BencodingException(murle.getLocalizedMessage());
}
//尝试提取信息字典
ByteBuffer info_bytes=Bencoder2.getInfoBytes(torrent_file_bytes);
Map info\u Map=(Map)this.torrent\u文件\u Map.get(TorrentInfo.KEY\u info);
如果(信息映射==null)
抛出新的BencodingException(“无法从torrent metainfo dictionary.Corrupt文件提取信息字典?”);
this.info\u map=info\u map;
//尝试生成信息哈希值
试一试{
MessageDigest=MessageDigest.getInstance(“SHA-1”);
update(info_bytes.array());
字节[]info_hash=digest.digest();
this.info\u hash=ByteBuffer.wrap(info\u hash);
}
捕获(无算法异常nsae)
{
抛出新的BencodingException(nsae.getLocalizedMessage());
}
//从信息字典中提取工件长度
整型片段长度=(整型)this.info\u map.get(TorrentInfo.KEY\u片段长度);
如果(工件长度==null)
抛出新的BencodingException(“无法从信息字典中提取片段长度。损坏的文件?”);
this.piece_length=piece_length.intValue();
//从信息字典中提取文件名
ByteBuffer name_bytes=(ByteBuffer)this.info_map.get(TorrentInfo.KEY_name);
if(name_bytes==null)
抛出新的BencodingException(“无法从信息字典中检索文件名。损坏的文件?”);
试一试{
this.file_name=新字符串(name_bytes.array(),“ASCII”);
}
捕获(不支持的编码异常uee)
{
抛出新的BencodingException(uee.getLocalizedMessage());
}
//从信息字典中提取文件长度
整数文件长度=(整数)this.info\u map.get(TorrentInfo.KEY\u length);
if(文件长度==null)
抛出新的BencodingException(“无法从信息字典中提取文件长度。损坏的文件?”);
this.file_length=file_length.intValue();
//从信息字典中提取片段哈希
ByteBuffer all_hashes=(ByteBuffer)this.info\u map.get(TorrentInfo.KEY\u片段);
if(所有_散列==null)
抛出新的BencodingException(“无法从信息字典中提取片段哈希。损坏的文件?”);
字节[]all_hashes_array=all_hashes.array();
//验证阵列的长度是否为20字节(160位)的倍数
if(所有散列数组长度%20!=0)
抛出新的BencodingException(“片段哈希长度不是20的倍数。损坏的文件?”);
int num\u pieces=all\u hashes\u array.length/20;
//将片段散列的值复制到本地字段中
this.piece_hashes=新的ByteBuffer[num_pieces];
对于(int i=0;i
}
读了两天代码后,我不知道我的错误在哪里,我非常感谢所有的帮助。谢谢。种子可以包含
udp://...
宣布用于的URL。当然,方案名称有点过于通用,但事实就是这样
默认情况下,Java的URL类无法处理这些问题。有关可能的解决方案,请参阅
有人可能会说这是库中的一个bug,因为它不只是返回字符串,所以torrent解码不会仅仅因为格式错误或未知的URI而中止。实现在接受什么方面应该是自由的(合理的),所以在这种情况下,它们应该简单地过滤掉它们无法处理的URI方案