java.lang.Object
org.klomp.snark.bencode.BEncoder
Converts Java objects to bencoded format for BitTorrent protocol communication.
Bencoding is a simple way to structure data used in BitTorrent. This class provides static methods to encode the following Java types:
- Strings - encoded as UTF-8 byte strings with length prefix
- byte[] - encoded as raw byte strings with length prefix
- Numbers - encoded as integer values with 'i' prefix and 'e' suffix
- List - encoded as ordered sequences starting with 'l' and ending with 'e'
- Map - encoded as dictionaries whose keys are sorted in raw byte order (a shorter key that is a prefix of a longer one first), starting with 'd' and ending with 'e'
- BEValue - unwrapped and encoded according to its contained type
Example output formats:
- String "hello" → "5:hello"
- Number 42 → "i42e"
- List ["a", "b"] → "l1:a1:be"
- Map {"key":"value"} → "d3:key5:valuee"
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classComparator for bencode map keys given as byte arrays.private static classCompares bencode map keys given as Strings, in the order the spec requires: as their UTF-8 encodings, byte by byte, with a shorter key that is a prefix of a longer one ordered first.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionstatic voidbencode(byte[] bs, OutputStream out) Bencode a byte array to the given OutputStream.static voidbencode(Number n, OutputStream out) Bencode a Number to the given OutputStream.static voidbencode(Object o, OutputStream out) Bencode an object to the given OutputStream.static voidbencode(String s, OutputStream out) Bencode a String to the given OutputStream.static voidbencode(List<?> l, OutputStream out) Bencode a List to the given OutputStream.static byte[]Bencode a Map to a byte array.static voidbencode(Map<?, ?> m, OutputStream out) Bencode a Map to the given OutputStream.private static intcompareBytes(byte[] lhs, byte[] rhs) Lexicographic unsigned comparison of two byte strings, as bencode requires for map keys: the first differing byte decides, and a key that is a prefix of the other sorts first because it is shorter.
Constructor Details
BEncoder
private BEncoder()Utility class with static methods only.
Method Details
bencode
Bencode an object to the given OutputStream.- Parameters:
o- the object to encode (String, byte[], Number, List, Map, or BEValue)out- the OutputStream to write the bencoded data to- Throws:
IOException- if an I/O error occursIllegalArgumentException- if the object type cannot be encodedNullPointerException- if the object is null
bencode
Bencode a String to the given OutputStream.- Parameters:
s- the String to encode (will be UTF-8 encoded)out- the OutputStream to write the bencoded data to- Throws:
IOException- if an I/O error occurs
bencode
Bencode a Number to the given OutputStream.- Parameters:
n- the Number to encodeout- the OutputStream to write the bencoded data to- Throws:
IOException- if an I/O error occurs
bencode
Bencode a List to the given OutputStream.- Parameters:
l- the List to encodeout- the OutputStream to write the bencoded data to- Throws:
IOException- if an I/O error occurs
bencode
Bencode a byte array to the given OutputStream.- Parameters:
bs- the byte array to encodeout- the OutputStream to write the bencoded data to- Throws:
IOException- if an I/O error occurs
bencode
Bencode a Map to a byte array. Keys must be Strings or (supported as of 0.9.31) byte[]s. A mix in the same Map is not supported.- Parameters:
m- the Map to encode- Returns:
- the bencoded byte array
- Throws:
IllegalArgumentException- if keys are not all Strings or all byte[]s
bencode
public static void bencode(Map<?, ?> m, OutputStream out) throws IOException, IllegalArgumentExceptionBencode a Map to the given OutputStream. Keys must be Strings or (supported as of 0.9.31) byte[]s. A mix in the same Map is not supported.- Parameters:
m- the Map to encodeout- the OutputStream to write the bencoded data to- Throws:
IOException- if an I/O error occursIllegalArgumentException- if keys are not all Strings or all byte[]s
compareBytes
private static int compareBytes(byte[] lhs, byte[] rhs) Lexicographic unsigned comparison of two byte strings, as bencode requires for map keys: the first differing byte decides, and a key that is a prefix of the other sorts first because it is shorter.DataHelper.compareTo()is not usable here: it orders keys of differing length by length alone, without comparing the common prefix, so it sorts "info" before "files".- Parameters:
lhs- the first keyrhs- the second key- Returns:
- negative if lhs is less, positive if greater, 0 if equal