public final class Bencode extends Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
Bencode.ThrowingConsumer<T> |
| Constructor and Description |
|---|
Bencode()
Create a new Bencoder using the default
Charset (UTF-8) and useBytes as false. |
Bencode(boolean useBytes)
Creates a new Bencoder using the boolean passed to control String parsing.
|
Bencode(Charset charset)
Creates a new Bencoder using the
Charset passed for encoding/decoding and useBytes as false. |
Bencode(Charset charset,
boolean useBytes)
Creates a new Bencoder using the
Charset passed for encoding/decoding and boolean passed to control String parsing. |
| Modifier and Type | Method and Description |
|---|---|
<T> T |
decode(byte[] bytes,
Type<T> type)
Decodes a bencode encoded byte array.
|
byte[] |
encode(Iterable<?> l)
Encodes the passed
Iterable as a bencode List. |
byte[] |
encode(Map<?,?> m)
Encodes the passed
Map as a bencode Dictionary. |
byte[] |
encode(Number n)
Encodes the passed
Number. |
byte[] |
encode(String s)
Encodes the passed
String. |
Charset |
getCharset()
Gets the
Charset the coder was created with. |
Type |
type(byte[] bytes)
Determines the first
Type contained within the byte array. |
public Bencode()
Charset (UTF-8) and useBytes as false.Bencode(Charset, boolean)public Bencode(Charset charset)
Charset passed for encoding/decoding and useBytes as false.charset - the Charset to useNullPointerException - if the Charset passed is nullBencode(Charset, boolean)public Bencode(boolean useBytes)
useBytes - Bencode(Charset, boolean)public Bencode(Charset charset, boolean useBytes)
Charset passed for encoding/decoding and boolean passed to control String parsing.
If useBytes is false, then dictionary values that contain byte string data will be coerced to a String.
if useBytes is true, then dictionary values that contain byte string data will be coerced to a ByteBuffer.charset - the Charset to useuseBytes - true to have dictionary byte data to stay as bytesNullPointerException - if the Charset passed is nullpublic Charset getCharset()
Charset the coder was created with.Charset of the coderpublic Type type(byte[] bytes)
Type contained within the byte array.bytes - the bytes to determine the Type forType or Type.UNKNOWN if it cannot be determinedNullPointerException - if bytes is nullBencodeException - if an error occurs during detectionpublic <T> T decode(byte[] bytes,
Type<T> type)
T - inferred from the Type parameterbytes - the bytes to decodetype - the Type to decode asNullPointerException - if bytes or type is nullIllegalArgumentException - if type is Type.UNKNOWNBencodeException - if an error occurs during decodingpublic byte[] encode(String s)
String.s - the String to encodeNullPointerException - if the String is nullBencodeException - if an error occurs during encodingpublic byte[] encode(Number n)
Number.
The number is converted to a Long, meaning any precision is lost as it not supported by the bencode spec.
n - the Number to encodeNullPointerException - if the Number is nullBencodeException - if an error occurs during encodingpublic byte[] encode(Iterable<?> l)
Iterable as a bencode List.
Data contained in the List is written as the correct type. Any Iterable is written as a List,
any Number as a Number, any Map as a Dictionary and any other Object is written as a String
calling the Object.toString() method.
l - the Iterable to encodeNullPointerException - if the List is nullBencodeException - if an error occurs during encodingpublic byte[] encode(Map<?,?> m)
Map as a bencode Dictionary.
Data contained in the Dictionary is written as the correct type. Any Iterable is written as a List,
any Number as a Number, any Map as a Dictionary and any other Object is written as a String
calling the Object.toString() method.
m - the Map to encodeNullPointerException - if the Map is nullBencodeException - if an error occurs during encodingCopyright © 2015–2025. All rights reserved.