-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToolKit.java
182 lines (172 loc) · 6.17 KB
/
ToolKit.java
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
179
180
181
182
/*
* RUBTClient is a BitTorrent client written at Rutgers University for
* instructional use.
* Copyright (C) 2008 Robert Moore II
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.util.*;
import java.nio.*;
/**
* Contains a number of methods useful for debugging and developing programs
* using Bencoding methods. This class is specifically designed to be used
* with <code>edu.rutgers.cs.cs352.bt.util.Bencoder</code> and
* <code>Bencoder2</code>.
*
* @author Robert Moore II
*
*/
public class ToolKit
{
/**
* Prints out Java versions of Bencoding data types.
* @param o the Object to print. It must be one of the following
* types: @
*/
@SuppressWarnings("unchecked")
public static void print(Object o)
{
if (o instanceof ByteBuffer)
printString((ByteBuffer) o, true, 0);
else if (o instanceof byte[])
printString((byte[]) o, true, 0);
else if (o instanceof Integer)
printInteger((Integer) o, 0);
else if (o instanceof ArrayList)
printList((ArrayList) o, 0);
else if (o instanceof HashMap)
printMap((HashMap) o, 0);
else
System.err.println("Error: Unknown type");
}
/**
* Prints the specified object with the provided depth. The depth simply indicates how much to indent.
* @param o the object to print
* @param depth the depth of the object within another data type.
*/
@SuppressWarnings("unchecked")
public static void print(Object o, int depth)
{
if (o instanceof ByteBuffer)
printString((ByteBuffer) o, true, depth + 1);
else if (o instanceof byte[])
printString((byte[]) o, true, depth + 1);
else if (o instanceof Integer)
printInteger((Integer) o, depth + 1);
else if (o instanceof ArrayList)
printList((ArrayList) o, depth + 1);
else if (o instanceof HashMap)
printMap((HashMap) o, depth + 1);
else
System.err.println("Error: Unknown type");
}
/**
* Helper method that prints out a byte string as a series of integer or ASCII
* characters.
* @param bytes the byte string to print.
* @param as_text {@code true} if the byte string should be printed as ASCII characters
* @param depth the depth of the object within other objects, used for indenting.
*/
public static void printString(byte[] bytes, boolean as_text, int depth)
{
for (int k = 0; k < depth; k++)
System.out.print(" ");
System.out.print("String: ");
for (int i = 0; i < bytes.length; i++)
{
System.out
.print(as_text ? (char) bytes[i] : (int) bytes[i] + " ");
}
System.out.println();
}
/**
* Helper method that prints out a {@code ByteBuffer} as a series of integer or ASCII
* characters.
* @param byte_string the {@code ByteBuffer} to print.
* @param as_text {@code true} if the {@code ByteBuffer} should be printed as ASCII characters
* @param depth the depth of the object within other objects, used for indenting.
*/
public static void printString(ByteBuffer byte_string, boolean as_text,
int depth)
{
for (int k = 0; k < depth; k++)
System.out.print(" ");
System.out.print("String: ");
byte[] bytes = byte_string.array();
for (int i = 0; i < bytes.length; i++)
{
System.out
.print(as_text ? (char) bytes[i] : (int) bytes[i] + " ");
}
System.out.println();
}
/**
* Helper method that prints out an integer.
* @param i the integer to print.
* @param depth the depth of the object within other objects, used for indenting.
*/
public static void printInteger(Integer i, int depth)
{
for (int k = 0; k < depth; k++)
System.out.print(" ");
System.out.println("Integer: " + i);
}
/**
* Helper method that prints out a list.
* @param list the list to print.
* @param depth the depth of the object within other objects, used for indenting.
*/
@SuppressWarnings("unchecked")
public static void printList(AbstractList list, int depth)
{
final Iterator i = list.iterator();
Object o = null;
for (int k = 0; k < depth; k++)
System.out.print(" ");
System.out.println("List: ");
while (i.hasNext() && (o = i.next()) != null)
{
for (int k = 0; k < depth; k++)
System.out.print(" ");
System.out.print(" +");
print(o, depth);
}
}
/**
* Helper method that prints out a dictionary/map.
* @param map the dictionary/map to print.
* @param depth the depth of the object within other objects, used for indenting.
*/
@SuppressWarnings("unchecked")
public static void printMap(Map map, int depth)
{
final Iterator i = map.keySet().iterator();
Object key = null;
for (int k = 0; k < depth; k++)
System.out.print(" ");
System.out.println("Dictionary:");
while (i.hasNext() && (key = i.next()) != null)
{
for (int k = 0; k < depth; k++)
System.out.print(" ");
System.out.print("(K) ");
print(key, depth);
Object val = map.get(key);
for (int k = 0; k < depth; k++)
System.out.print(" ");
System.out.print("(V) ");
print(val, depth);
}
}
}