Package com.biglybt.core.util
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 class
static interface
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private byte[]
private ByteBuffer
private int
private CharBuffer
private final Charset
private Charset
private final CharsetDecoder
private CharsetDecoder
private BDecoder.MapDecodeListener
static final int
private static final int
private static final boolean
private 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 boolean
private static final boolean
private boolean
private 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 Map
decodeFromJSON
(String json) static List
decodeFromJSONArray
(List j_list) private static Object
static Map
decodeFromJSONObject
(Map<Object, Object> j_map) private Object
decodeInputStream
(InputStream dbis, String context, int nesting, boolean internKeys) private Object
decodeInputStream2
(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 List
decodeStrings
(List list) Decodes byte arrays into strings.static Map
decodeStrings
(Map map) Converts any byte[] entries into UTF-8 strings.private void
getByteArrayFromStream
(InputStream dbis, int length, byte[] targetArray) private byte[]
getByteArrayFromStream
(InputStream dbis, String context) private long
getNumberFromStream
(InputStream dbis, char parseChar) private int
getPositiveNumberFromStream
(InputStream dbis, char parseChar) static void
static long
parseLong
(char[] chars, int start, int length) private static void
static void
print
(PrintWriter writer, Object obj) private static void
print
(PrintWriter writer, Object obj, String indent, boolean skip_indent) static void
void
setMapDecodeListener
(BDecoder.MapDecodeListener mapDecodeListener) void
setRecoveryMode
(boolean r) void
setVerifyMapOrder
(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
Converts any byte[] entries into UTF-8 strings. REPLACES EXISTING MAP VALUES- Parameters:
map
-- Returns:
-
decodeStrings
Decodes byte arrays into strings. REPLACES EXISTING LIST VALUES- Parameters:
list
-- Returns:
- the same list passed in
-
print
-
decodeFromJSONGeneric
-
decodeFromJSONArray
-
decodeFromJSONObject
-
decodeFromJSON
-
getMapDecodeListener
-
setMapDecodeListener
-
main
-