Class HuffmanEncoder
Used to encode strings Huffman encoding.
Characters are encoded with ISO-8859-1, if any multi-byte characters or
control characters are present the encoder will throw EncodingException.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidencode(ByteBuffer buffer, String s) static voidencodeLowerCase(ByteBuffer buffer, String s) static voidencodeLowerCaseWithLength(ByteBuffer buffer, int prefix, String s) AsencodeWithLength(ByteBuffer, int, String), encodingsin lowercase.static voidencodeWithLength(ByteBuffer buffer, int prefix, String s) Encodes, preceded by the number of octets it encodes to, written as an n-bit integer with the givenprefix.static intoctetsNeeded(byte[] b) static intstatic int
-
Method Details
-
octetsNeeded
- Parameters:
s- the string to encode.- Returns:
- the number of octets needed to encode the string, or -1 if it cannot be encoded.
-
octetsNeeded
public static int octetsNeeded(byte[] b) - Parameters:
b- the byte array to encode.- Returns:
- the number of octets needed to encode the bytes, or -1 if it cannot be encoded.
-
encode
- Parameters:
buffer- the buffer to encode into.s- the string to encode.
-
octetsNeededLowerCase
- Parameters:
s- the string to encode in lowercase.- Returns:
- the number of octets needed to encode the string, or -1 if it cannot be encoded.
-
encodeLowerCase
- Parameters:
buffer- the buffer to encode into in lowercase.s- the string to encode.
-
encodeWithLength
Encode
s, preceded by the number of octets it encodes to, written as an n-bit integer with the givenprefix.The length precedes the content but is not known until the content has been encoded, which would need a separate pass over
sto compute. Instead the content is encoded first, into the space after the octet that holds the length, and the length is then filled in. That octet only has room for a length below the prefix maximum, which every string of fewer than(1 << prefix) - 1encoded octets satisfies; for the rare longer string the buffer is rewound and both are written the direct way.- Parameters:
buffer- the buffer to encode intoprefix- the prefix used to encode the length, in bitss- the string to encode
-
encodeLowerCaseWithLength
AsencodeWithLength(ByteBuffer, int, String), encodingsin lowercase.- Parameters:
buffer- the buffer to encode intoprefix- the prefix used to encode the length, in bitss- the string to encode
-