资源简介
Java解析bt torrent种子文件的工具类代码分享,只能用炫酷来形容,感兴趣就下载看看吧
代码片段和文件信息
package download.bt;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* 解析bt种子
*/
@SuppressWarnings(value = { “rawtypes“ “unchecked“ “unused“ })
public class BDecoder {
public static Charset BYTE_CHARSET = Charset.forName(“UTF-8“);;
public static Charset DEFAULT_CHARSET = Charset.forName(“UTF-8“);;
private boolean recovery_mode;
public static Map decode(byte[] data) throws IOException {
return (new BDecoder().decodeByteArray(data));
}
public static Map decode(BufferedInputStream is) throws IOException {
return (new BDecoder().decodeStream(is));
}
public BDecoder() {
}
public Map decodeByteArray(byte[] data) throws IOException {
return (decode(new ByteArrayInputStream(data)));
}
public Map decodeStream(BufferedInputStream data) throws IOException {
object res = decodeInputStream(data 0);
if (res == null) {
throw (new IOException(“BDecoder: zero length file“));
} else if (!(res instanceof Map)) {
throw (new IOException(“BDecoder: top level isn‘t a Map“));
}
return ((Map) res);
}
private Map decode(ByteArrayInputStream data) throws IOException {
object res = decodeInputStream(data 0);
if (res == null) {
throw (new IOException(“BDecoder: zero length file“));
} else if (!(res instanceof Map)) {
throw (new IOException(“BDecoder: top level isn‘t a Map“));
}
return ((Map) res);
}
private object decodeInputStream(InputStream bais int nesting)
throws IOException {
if (nesting == 0 && !bais.markSupported()) {
throw new IOException(“InputStream must support the mark() method“);
}
// set a mark
bais.mark(Integer.MAX_VALUE);
// read a byte
int tempByte = bais.read();
// decide what to do
switch (tempByte) {
case ‘d‘:
// create a new dictionary object
Map tempMap = new HashMap();
try {
// get the key
byte[] tempByteArray = null;
while ((tempByteArray = (byte[]) decodeInputStream(bais nesting + 1)) != null) {
// decode some more
object value = decodeInputStream(bais nesting + 1);
// add the value to the map
CharBuffer cb = BYTE_CHARSET.decode(ByteBuffer.wrap(tempByteArray));
String key = new String(cb.array() 0 cb.limit());
tempMap.put(key value);
}
bais.mark(Integer.MAX_VALUE);
tempByte = bais.read();
bais.reset();
if (nesting > 0 && tempByte == -1) {
throw (new IOException(“BDecoder: invalid input data ‘e‘ missing from end of dictionary“));
}
} catch (Throwable e) {
if (!recovery_mode) {
相关资源
- jsonarray所必需的6个jar包.rar
- 三角网构TIN生成算法,Java语言实现
- java代码编写将excel数据导入到mysql数据
- Java写的cmm词法分析器源代码及javacc学
- JAVA JSP公司财务管理系统 源代码 论文
- JSP+MYSQL旅行社管理信息系统
- 推荐算法的JAVA实现
- 基于Java的酒店管理系统源码(毕业设
- java-图片识别 图片比较
- android毕业设计
- java23种设计模式+23个实例demo
- java Socket发送/接受报文
- JAVA828436
- java界面美化 提供多套皮肤直接使用
- 在线聊天系统(java代码)
- 基于Java的图书管理系统807185
- java中实现将页面数据导入Excel中
- java 企业销售管理系统
- java做的聊天系统(包括正规课程设计
- Java编写的qq聊天室
- 商店商品管理系统 JAVA写的 有界面
- JAVA开发聊天室程序
- 在linux系统下用java执行系统命令实例
- java期末考试试题两套(答案) 选择(
- JAVA3D编程示例(建模、交互)
- Java 文件加密传输
- java做的房产管理系统
- 基于jsp的bbs论坛 非常详细
- [免费]java实现有障碍物的贪吃蛇游戏
- java Servlet投票实例
评论
共有 条评论