Class BDecoder
java.lang.Object
com.biglybt.core.util.BDecoder
A set of utility methods to decode a bencoded array of byte into a Map.
integer are represented as Long, String as byte[], dictionaries as Map, and list as List.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classstatic interface -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate byte[]private ByteBufferprivate intprivate CharBufferprivate final Charsetprivate Charsetprivate final CharsetDecoderprivate CharsetDecoderprivate BDecoder.MapDecodeListenerstatic final intprivate static final intprivate static final booleanprivate final char[]only create the array once per decoder instance (no issues with recursion as it's only used in a leaf method)private static final byte[]private booleanprivate static final booleanprivate booleanprivate boolean -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondecode(byte[] data) decode(byte[] data, int offset, int length) decode(InputStream data, boolean internKeys) decodeByteArray(byte[] data) decodeByteArray(byte[] data, int offset, int length) decodeByteArray(byte[] data, int offset, int length, boolean internKeys) decodeByteBuffer(ByteBuffer buffer, boolean internKeys) static MapdecodeFromJSON(String json) static ListdecodeFromJSONArray(List j_list) private static Objectstatic MapdecodeFromJSONObject(Map<Object, Object> j_map) private ObjectdecodeInputStream(InputStream dbis, String context, int nesting, boolean internKeys) private ObjectdecodeInputStream2(InputStream dbis, String context, int nesting, boolean internKeys) Differences between decodeInputStream
This: Uses byte[] for keys
Other: Uses ByteBuffer
This: Uses new String(byte[], pos, len, charset)
Other: Uses CharsetDecoder.decode(ByteBuffer, CharBuffer) and new String(charBuffer.array(), 0, len)
This: Verifies map order using key in String form
Other: Verifies map order by copying bytes and comparingdecodeStream(BufferedInputStream data, boolean internKeys) static ListdecodeStrings(List list) Decodes byte arrays into strings.static MapdecodeStrings(Map map) Converts any byte[] entries into UTF-8 strings.private voidgetByteArrayFromStream(InputStream dbis, int length, byte[] targetArray) private byte[]getByteArrayFromStream(InputStream dbis, String context) private longgetNumberFromStream(InputStream dbis, char parseChar) private intgetPositiveNumberFromStream(InputStream dbis, char parseChar) static voidstatic longparseLong(char[] chars, int start, int length) private static voidstatic voidprint(PrintWriter writer, Object obj) private static voidprint(PrintWriter writer, Object obj, String indent, boolean skip_indent) static voidvoidsetMapDecodeListener(BDecoder.MapDecodeListener mapDecodeListener) voidsetRecoveryMode(boolean r) voidsetVerifyMapOrder(boolean b)
-
Field Details
-
MAX_BYTE_ARRAY_SIZE
public static final int MAX_BYTE_ARRAY_SIZE- See Also:
-
MAX_MAP_KEY_SIZE
private static final int MAX_MAP_KEY_SIZE- See Also:
-
NEWDECODER_FOR_DEF_CHARSET
private static final boolean NEWDECODER_FOR_DEF_CHARSET -
TRACE
private static final boolean TRACE- See Also:
-
PORTABLE_ROOT
private static final byte[] PORTABLE_ROOT -
recovery_mode
private boolean recovery_mode -
verify_map_order
private boolean verify_map_order -
useNewDecoder
private boolean useNewDecoder -
keyDecoder
-
keyCharset
-
keyDecoderUTF8
-
keyCharsetUTF8
-
keyBytesLen
private int keyBytesLen -
keyBytesBuffer
-
keyBytes
private byte[] keyBytes -
keyCharsBuffer
-
mapDecodeListener
-
force_utf8_keys
private boolean force_utf8_keys -
numberChars
private final char[] numberCharsonly create the array once per decoder instance (no issues with recursion as it's only used in a leaf method)
-
-
Constructor Details
-
BDecoder
public BDecoder()Create a BDecoder using BYTE_ENCODING_CHARSET (ISO_8859_1) -
BDecoder
Create a BDecoder using specified charset. New decoder will be used, which can handle UTF-8 properly -
BDecoder
-
-
Method Details
-
decode
- Throws:
IOException
-
decode
- Throws:
IOException
-
decode
- Throws:
IOException
-
decodeByteArray
- Throws:
IOException
-
decodeByteArray
- Throws:
IOException
-
decodeByteArray
public Map<String,Object> decodeByteArray(byte[] data, int offset, int length, boolean internKeys) throws IOException - Throws:
IOException
-
decodeByteBuffer
public Map<String,Object> decodeByteBuffer(ByteBuffer buffer, boolean internKeys) throws IOException - Throws:
IOException
-
decodeStream
- Throws:
IOException
-
decodeStream
public Map<String,Object> decodeStream(BufferedInputStream data, boolean internKeys) throws IOException - Throws:
IOException
-
decode
- Throws:
IOException
-
decodeInputStream
private Object decodeInputStream(InputStream dbis, String context, int nesting, boolean internKeys) throws IOException - Throws:
IOException
-
decodeInputStream2
private Object decodeInputStream2(InputStream dbis, String context, int nesting, boolean internKeys) throws IOException Differences between decodeInputStream
This: Uses byte[] for keys
Other: Uses ByteBuffer
This: Uses new String(byte[], pos, len, charset)
Other: Uses CharsetDecoder.decode(ByteBuffer, CharBuffer) and new String(charBuffer.array(), 0, len)
This: Verifies map order using key in String form
Other: Verifies map order by copying bytes and comparing
On a torrent with 100k files and set to verify map order, this method is 2.5x faster.- Throws:
IOException
-
getPositiveNumberFromStream
- Throws:
IOException
-
getNumberFromStream
- Throws:
IOException
-
parseLong
public static long parseLong(char[] chars, int start, int length) -
getByteArrayFromStream
- Throws:
IOException
-
getByteArrayFromStream
private void getByteArrayFromStream(InputStream dbis, int length, byte[] targetArray) throws IOException - Throws:
IOException
-
setVerifyMapOrder
public void setVerifyMapOrder(boolean b) -
setRecoveryMode
public void setRecoveryMode(boolean r) -
print
-
print
-
print
-
decodeStrings
-
decodeStrings
-
print
-
decodeFromJSONGeneric
-
decodeFromJSONArray
-
decodeFromJSONObject
-
decodeFromJSON
-
getMapDecodeListener
-
setMapDecodeListener
-
main
-