merge of '3f93d2c09c89b5c68487c33fd700ef7c2feeeb61'

and 'b2c58f8462ab5c08682b711436c387b421bdd0c2'
This commit is contained in:
zzz
2013-12-10 11:11:08 +00:00

View File

@ -155,7 +155,7 @@ public class BEncoder
out.write(bs); out.write(bs);
} }
public static byte[] bencode(Map<String, Object> m) public static byte[] bencode(Map<?, ?> m)
{ {
try try
{ {
@ -169,23 +169,29 @@ public class BEncoder
} }
} }
public static void bencode(Map<String, Object> m, OutputStream out) throws IOException public static void bencode(Map<?, ?> m, OutputStream out)
throws IOException, IllegalArgumentException
{ {
out.write('d'); out.write('d');
// Keys must be sorted. XXX - But is this the correct order? // Keys must be sorted. XXX - But is this the correct order?
Set<String> s = m.keySet(); Set<?> s = m.keySet();
List<String> l = new ArrayList<String>(s); List<String> l = new ArrayList<String>(s.size());
for (Object k : s) {
// Keys must be Strings.
if (String.class.isAssignableFrom(k.getClass()))
l.add((String) k);
else
throw new IllegalArgumentException("Cannot bencode map: contains non-String key of type " + k.getClass());
}
Collections.sort(l); Collections.sort(l);
Iterator<String> it = l.iterator(); Iterator<String> it = l.iterator();
while(it.hasNext()) while(it.hasNext())
{ {
// Keys must be Strings.
String key = it.next(); String key = it.next();
Object value = m.get(key);
bencode(key, out); bencode(key, out);
bencode(value, out); bencode(m.get(key), out);
} }
out.write('e'); out.write('e');