writeBytes( encoded );
}else{
ByteBuffer bb = Constants.DEFAULT_CHARSET.encode( tempString );
writeInt( bb.limit() );
writeChar(':');
writeByteBuffer(bb );
}
}else if(object instanceof Map){
Map tempMap = (Map)object;
SortedMap tempTree = null;
// unfortunately there are some occasions where we want to ensure that
// the 'key' of the map is not mangled by assuming its UTF-8 encodable.
// In particular the response from a tracker scrape request uses the
// torrent hash as the KEY. Hence the introduction of the type below
// to allow the constructor of the Map to indicate that the keys should
// be extracted using a BYTE_ENCODING
boolean byte_keys = object instanceof ByteEncodedKeyHashMap;
//write the d
writeChar('d');
//are we sorted?
if ( tempMap instanceof TreeMap ){
tempTree = (TreeMap)tempMap;
}else{
tempTree = new TreeMap(tempMap);
}
Iterator it = tempTree.entrySet().iterator();
while( it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
Object o_key = entry.getKey();
Object value = entry.getValue();
if (value != null)
{
if (o_key instanceof byte[])
{
encodeObject(o_key);
if (!encodeObject(value))
encodeObject("");
} else if(o_key instanceof String)
{
String key = (String) o_key;
if (byte_keys)
{
try
{
encodeObject(Constants.BYTE_CHARSET.encode(key));
if (!encodeObject(value))
encodeObject("");
} catch (UnsupportedEncodingException e)
{
throw (new IOException("BEncoder: unsupport encoding: " + e.getMessage()));
}
} else
{
// if we put non-ascii chars in as keys we can get horrible expanding
// config issues as we cycle through decode/encode cycles with certain
// characters
if ( Constants.IS_CVS_VERSION ){
char[] chars = key.toCharArray();
for ( char c: chars ){
if (c >= '\u0080'){
if ( non_ascii_logs < 50 ){
non_ascii_logs++;
Debug.out( "Non-ASCII key: " + key );
}
break;
}
}
}
encodeObject(key); // Key goes in as UTF-8
if (!encodeObject(value))
encodeObject("");
}
} else
Debug.out( "Attempt to encode an unsupported map key type: " + object.getClass() + ";value=" + object);
}
}
writeChar('e');
}else if(object instanceof List){
List tempList = (List)object;
//write out the l
writeChar('l');
for(int i = 0; i<tempList.size(); i++){
encodeObject( tempList.get(i));
}
writeChar('e');
}else if(object instanceof Long){
Long tempLong = (Long)object;
//write out the l
writeChar('i');
writeLong(tempLong.longValue());
writeChar('e');
}else if(object instanceof Integer){
Integer tempInteger = (Integer)object;
//write out the l
writeChar('i');
writeInt(tempInteger.intValue());
writeChar('e');
}else if(object instanceof byte[]){
byte[] tempByteArray = (byte[])object;
writeInt(tempByteArray.length);
writeChar(':');
if ( url_encode ){
writeBytes(URLEncoder.encode(new String(tempByteArray, Constants.BYTE_ENCODING), Constants.BYTE_ENCODING ).getBytes());
}else{
writeBytes(tempByteArray);
}
}else if(object instanceof ByteBuffer ){
ByteBuffer bb = (ByteBuffer)object;
writeInt(bb.limit());
writeChar(':');
writeByteBuffer(bb);
}else if ( object == null ){