Package org.apache.commons.codec.binary
Class Base16
java.lang.Object
org.apache.commons.codec.binary.BaseNCodec
org.apache.commons.codec.binary.Base16
- All Implemented Interfaces:
BinaryDecoder,BinaryEncoder,Decoder,Encoder
Provides Base16 encoding and decoding.
This class is thread-safe.
This implementation strictly follows RFC 4648, and as such unlike
the Base32 and Base64 implementations,
it does not ignore invalid alphabet characters or whitespace,
neither does it offer chunking or padding characters.
The only additional feature above those specified in RFC 4648 is support for working with a lower-case alphabet in addition to the default upper-case alphabet.
- Since:
- 1.15
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.commons.codec.binary.BaseNCodec
BaseNCodec.Context -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intBASE16 characters are 4 bits in length.private static final intprivate static final intprivate final byte[]Decode table to use.private final byte[]Encode table to use.private static final byte[]This array is a lookup table that translates Unicode characters drawn from the a lower-case "Base16 Alphabet" into their 4-bit positive integer equivalents.private static final byte[]This array is a lookup table that translates 4-bit positive integer index values into their "Base16 Alphabet" lower-case equivalents.private static final intMask used to extract 4 bits, used when decoding character.private static final byte[]This array is a lookup table that translates Unicode characters drawn from the "Base16 Alphabet" (as specified in Table 5 of RFC 4648) into their 4-bit positive integer equivalents.private static final byte[]This array is a lookup table that translates 4-bit positive integer index values into their "Base16 Alphabet" equivalents as specified in Table 5 of RFC 4648.Fields inherited from class org.apache.commons.codec.binary.BaseNCodec
CHUNK_SEPARATOR, DECODING_POLICY_DEFAULT, EOF, lineLength, MASK_8BITS, MIME_CHUNK_SIZE, pad, PAD, PAD_DEFAULT, PEM_CHUNK_SIZE -
Constructor Summary
ConstructorsConstructorDescriptionBase16()Creates a Base16 codec used for decoding and encoding.Base16(boolean lowerCase) Creates a Base16 codec used for decoding and encoding.Base16(boolean lowerCase, CodecPolicy decodingPolicy) Creates a Base16 codec used for decoding and encoding. -
Method Summary
Modifier and TypeMethodDescription(package private) voiddecode(byte[] data, int offset, int length, BaseNCodec.Context context) private intdecodeOctet(byte octet) (package private) voidencode(byte[] data, int offset, int length, BaseNCodec.Context context) booleanisInAlphabet(byte octet) Returns whether or not theoctetis in the Base16 alphabet.private voidValidates whether decoding allows an entire final trailing character that cannot be used for a complete byte.Methods inherited from class org.apache.commons.codec.binary.BaseNCodec
available, containsAlphabetOrPad, decode, decode, decode, encode, encode, encode, encodeAsString, encodeToString, ensureBufferSize, getChunkSeparator, getCodecPolicy, getDefaultBufferSize, getEncodedLength, hasData, isInAlphabet, isInAlphabet, isStrictDecoding, isWhiteSpace, readResults
-
Field Details
-
BITS_PER_ENCODED_BYTE
private static final int BITS_PER_ENCODED_BYTEBASE16 characters are 4 bits in length. They are formed by taking an 8-bit group, which is converted into two BASE16 characters.- See Also:
-
BYTES_PER_ENCODED_BLOCK
private static final int BYTES_PER_ENCODED_BLOCK- See Also:
-
BYTES_PER_UNENCODED_BLOCK
private static final int BYTES_PER_UNENCODED_BLOCK- See Also:
-
UPPER_CASE_DECODE_TABLE
private static final byte[] UPPER_CASE_DECODE_TABLEThis array is a lookup table that translates Unicode characters drawn from the "Base16 Alphabet" (as specified in Table 5 of RFC 4648) into their 4-bit positive integer equivalents. Characters that are not in the Base16 alphabet but fall within the bounds of the array are translated to -1. -
UPPER_CASE_ENCODE_TABLE
private static final byte[] UPPER_CASE_ENCODE_TABLEThis array is a lookup table that translates 4-bit positive integer index values into their "Base16 Alphabet" equivalents as specified in Table 5 of RFC 4648. -
LOWER_CASE_DECODE_TABLE
private static final byte[] LOWER_CASE_DECODE_TABLEThis array is a lookup table that translates Unicode characters drawn from the a lower-case "Base16 Alphabet" into their 4-bit positive integer equivalents. Characters that are not in the Base16 alphabet but fall within the bounds of the array are translated to -1. -
LOWER_CASE_ENCODE_TABLE
private static final byte[] LOWER_CASE_ENCODE_TABLEThis array is a lookup table that translates 4-bit positive integer index values into their "Base16 Alphabet" lower-case equivalents. -
MASK_4BITS
private static final int MASK_4BITSMask used to extract 4 bits, used when decoding character.- See Also:
-
decodeTable
private final byte[] decodeTableDecode table to use. -
encodeTable
private final byte[] encodeTableEncode table to use.
-
-
Constructor Details
-
Base16
public Base16()Creates a Base16 codec used for decoding and encoding. -
Base16
public Base16(boolean lowerCase) Creates a Base16 codec used for decoding and encoding.- Parameters:
lowerCase- iftruethen use a lower-case Base16 alphabet.
-
Base16
Creates a Base16 codec used for decoding and encoding.- Parameters:
lowerCase- iftruethen use a lower-case Base16 alphabet.decodingPolicy- Decoding policy.
-
-
Method Details
-
decode
- Specified by:
decodein classBaseNCodec
-
decodeOctet
private int decodeOctet(byte octet) -
encode
- Specified by:
encodein classBaseNCodec
-
isInAlphabet
public boolean isInAlphabet(byte octet) Returns whether or not theoctetis in the Base16 alphabet.- Specified by:
isInAlphabetin classBaseNCodec- Parameters:
octet- The value to test.- Returns:
trueif the value is defined in the the Base16 alphabetfalseotherwise.
-
validateTrailingCharacter
private void validateTrailingCharacter()Validates whether decoding allows an entire final trailing character that cannot be used for a complete byte.- Throws:
IllegalArgumentException- if strict decoding is enabled
-