-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBencoding.java
More file actions
178 lines (170 loc) · 6.06 KB
/
Bencoding.java
File metadata and controls
178 lines (170 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Bencoding {
public Bencoding() {
}
private static String encode(Object valueToEncode) throws Exception {
if(valueToEncode instanceof ByteBuffer) {
byte[] byteString = ((ByteBuffer) valueToEncode).array();
return encode(Integer.toString(byteString.length).getBytes());
}
else if(valueToEncode instanceof Integer) {
return encode((Integer)valueToEncode);
}
else if(valueToEncode instanceof Long) {
return encode((Long)valueToEncode);
}
else if(valueToEncode instanceof List) {
return encode((List)valueToEncode);
}
else if(valueToEncode instanceof Map) {
return encode((Map)valueToEncode);
}
else
throw new Exception("Object type is not supported " + valueToEncode.getClass().getName());
}
public static String encode(String stringToEncode) {
return(stringToEncode.length() + ":" + stringToEncode);
}
public static String encode(Integer intToEncode) {
return("i" + Integer.toString(intToEncode) + "e");
}
public static String encode(Long longToEncode) {
return("i" + Long.toString(longToEncode) + "e");
}
public static String encode(List listToEncode) throws Exception {
Iterator i = listToEncode.iterator();
StringBuilder encodedString = new StringBuilder();
encodedString.append("l");
while(i.hasNext()) {
try {
Object o = i.next();
encodedString.append(encode(o));
}
catch(Exception ex) {
throw new Exception("Exception in encoding the list", ex);
}
}
encodedString.append("e");
return(encodedString.toString());
}
public static String encode(Map dicToEncode) throws Exception {
StringBuilder encodedString = new StringBuilder();
encodedString.append('d');
try {
TreeMap sortedMap = new TreeMap(dicToEncode);
Iterator i = sortedMap.entrySet().iterator();
while(i.hasNext()) {
Map.Entry pairs = (Map.Entry)i.next();
if(!(pairs.getKey() instanceof String)){
throw new Exception("Keys in dictionary was not a" +
"string.");
}
encodedString.append(encode((String)pairs.getKey()));
encodedString.append(encode(pairs.getValue()));
}
}
catch(Exception ex) {
throw new Exception("Exception in enoding the dictionary", ex);
}
encodedString.append('e');
return(encodedString.toString());
}
public static HashMap<String, Object> decodeDictionary(String encodedValue) throws Exception {
char nextChar = encodedValue.charAt(0);
encodedValue = encodedValue.substring(1);
if (nextChar != 'd')
throw new Exception("The next item in the bencoded string is not a Dictionary.");
HashMap<String, Object> dictionary = new HashMap<>();
while (encodedValue.charAt(0) != 'e') {
String key = decodeString(encodedValue);
char token = encodedValue.charAt(0);
Object value = null;
if (token == 'i')
value = decodeInteger(encodedValue);
else if (token == 'l')
value = decodeList(encodedValue);
else if (token == 'd')
value = decodeDictionary(encodedValue);
else
value = decodeString(encodedValue);
dictionary.put(key, value);
}
nextChar = encodedValue.charAt(0);
encodedValue = encodedValue.substring(1);
return dictionary;
}
public static ArrayList<Object> decodeList(String encodedValue) throws Exception {
char nextChar = encodedValue.charAt(0);
encodedValue = encodedValue.substring(1);
if (nextChar != 'l')
throw new Exception("The next item in the bencoded string is not a List.");
ArrayList<Object> list = new ArrayList<Object>();
while (encodedValue.charAt(0) != 'e') {
char token = encodedValue.charAt(0);
if (token == 'i')
list.add(decodeInteger(encodedValue));
else if (token == 'd')
list.add(decodeDictionary(encodedValue));
else if (token == 'l')
list.add(decodeList(encodedValue));
else
list.add(decodeString(encodedValue));
}
nextChar = encodedValue.charAt(0);
encodedValue = encodedValue.substring(1);
return list;
}
public static Object decodeInteger(String encodedValue) throws Exception {
char nextChar = encodedValue.charAt(0);
encodedValue = encodedValue.substring(1);
if (nextChar != 'i')
throw new Exception("The next item in the bencoded string is not an Integer.");
String[] data = encodedValue.split("e");
Object o = null;
if (isInt(data[0]))
o = Integer.parseInt(data[0]);
else
o = Long.parseLong(data[0]);
encodedValue = encodedValue.substring(data[0].length() + 1);
return o;
}
public static String decodeString(String encodedValue) throws Exception {
String data[] = encodedValue.split(":", 2);
int headerLength = data[0].length() + 1;
if (isInt(data[0])) {
int stringLength = Integer.parseInt(data[0]);
String string = data[1].substring(0, stringLength);
encodedValue = encodedValue.substring(headerLength + stringLength);
return string;
} else {
throw new Exception("The next item in the bencoded string is not a String. " + encodedValue);
}
}
private static boolean isInt(String strData){
try {
Integer.parseInt(strData);
return true;
} catch (Exception e) {
return false;
}
}
}