Class Base32
- All Implemented Interfaces:
BinaryDecoder,BinaryEncoder,Decoder,Encoder
The class can be parameterized in the following manner with various constructors:
- Whether to use the "base32hex" variant instead of the default "base32"
- Line length: Default 76. Line length that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
- Line separator: Default is CRLF ("\r\n")
This class operates directly on byte streams, and not character streams.
This class is thread-safe.
To configure a new instance, use a Base32.Builder. For example:
Base32 base32 = Base32.builder()
.setDecodingPolicy(DecodingPolicy.LENIENT) // default is lenient
.setLineLength(0) // default is none
.setLineSeparator('\r', '\n') // default is CR LF
.setPadding('=') // default is '='
.setEncodeTable(customEncodeTable) // default is RFC 4648 Section 6, Table 3: The Base 32 Alphabet
.get()
- Since:
- 1.5
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class org.apache.commons.codec.binary.BaseNCodec
BaseNCodec.AbstractBuilder<T,B extends BaseNCodec.AbstractBuilder<T, B>>, BaseNCodec.Context -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intBASE32 characters are 5 bits in length.private static final intprivate static final intprivate static final byte[]This array is a lookup table that translates Unicode characters drawn from the "Base32 Alphabet" (as specified in Table 3 of RFC 4648) into their 5-bit positive integer equivalents.private static final byte[]This array is a lookup table that translates 5-bit positive integer index values into their "Base32 Alphabet" equivalents as specified in RFC 4648 Section 6, Table 3: The Base 32 Alphabet.private final intConvenience variable to help us determine when our buffer is going to run out of room and needs resizing.private static final byte[]This array is a lookup table that translates Unicode characters drawn from the "Base32 Hex Alphabet" (as specified in Table 4 of RFC 4648) into their 5-bit positive integer equivalents.private static final byte[]This array is a lookup table that translates 5-bit positive integer index values into their "Base 32 Encoding with Extended Hex Alphabet" equivalents as specified in RFC 4648 Section 7, Table 4: Base 32 Encoding with Extended Hex Alphabet.private final byte[]Line separator for encoding.private static final longMask used to extract 1 bits, used when decoding final trailing character.private static final longMask used to extract 2 bits, used when decoding final trailing character.private static final longMask used to extract 3 bits, used when decoding final trailing character.private static final longMask used to extract 4 bits, used when decoding final trailing character.private static final intMask used to extract 5 bits, used when encoding Base32 bytesFields inherited from class org.apache.commons.codec.binary.BaseNCodec
CHUNK_SEPARATOR, decodeTable, DECODING_POLICY_DEFAULT, EMPTY_BYTE_ARRAY, encodeTable, EOF, lineLength, MASK_8BITS, MIME_CHUNK_SIZE, pad, PAD, PAD_DEFAULT, PEM_CHUNK_SIZE -
Constructor Summary
ConstructorsModifierConstructorDescriptionBase32()Constructs a Base32 codec used for decoding and encoding.Base32(boolean useHex) Deprecated.Base32(boolean useHex, byte padding) Deprecated.Usebuilder()andBase32.Builder.Base32(byte pad) Deprecated.Usebuilder()andBase32.Builder.Base32(int lineLength) Deprecated.Usebuilder()andBase32.Builder.Base32(int lineLength, byte[] lineSeparator) Deprecated.Usebuilder()andBase32.Builder.Base32(int lineLength, byte[] lineSeparator, boolean useHex) Deprecated.Usebuilder()andBase32.Builder.Base32(int lineLength, byte[] lineSeparator, boolean useHex, byte padding) Deprecated.Usebuilder()andBase32.Builder.Base32(int lineLength, byte[] lineSeparator, boolean useHex, byte padding, CodecPolicy decodingPolicy) Deprecated.Usebuilder()andBase32.Builder.privateBase32(Base32.Builder builder) -
Method Summary
Modifier and TypeMethodDescriptionstatic Base32.Builderbuilder()Creates a new Builder.(package private) voiddecode(byte[] input, int inPos, int inAvail, BaseNCodec.Context context) Decodes all of the provided data, starting at inPos, for inAvail bytes.private static byte[]decodeTable(boolean useHex) (package private) voidencode(byte[] input, int inPos, int inAvail, BaseNCodec.Context context) Encodes all of the provided data, starting at inPos, for inAvail bytes.private static byte[]encodeTable(boolean useHex) Gets the encoding table that matchesuseHex.(package private) byte[]Gets the line separator (for testing only).booleanisInAlphabet(byte octet) Returns whether or not theoctetis in the Base32 alphabet.private voidvalidateCharacter(long emptyBitsMask, BaseNCodec.Context context) Validates whether decoding the final trailing character is possible in the context of the set of possible Base32 values.private voidValidates whether decoding allows final trailing characters that cannot be created during encoding.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, getLength, hasData, isInAlphabet, isInAlphabet, isInAlphabet, isStrictDecoding, isWhiteSpace, readResults
-
Field Details
-
BITS_PER_ENCODED_BYTE
private static final int BITS_PER_ENCODED_BYTEBASE32 characters are 5 bits in length. They are formed by taking a block of five octets to form a 40-bit string, which is converted into eight BASE32 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:
-
DECODE_TABLE
private static final byte[] DECODE_TABLEThis array is a lookup table that translates Unicode characters drawn from the "Base32 Alphabet" (as specified in Table 3 of RFC 4648) into their 5-bit positive integer equivalents. Characters that are not in the Base32 alphabet but fall within the bounds of the array are translated to -1. -
ENCODE_TABLE
private static final byte[] ENCODE_TABLEThis array is a lookup table that translates 5-bit positive integer index values into their "Base32 Alphabet" equivalents as specified in RFC 4648 Section 6, Table 3: The Base 32 Alphabet. -
HEX_DECODE_TABLE
private static final byte[] HEX_DECODE_TABLEThis array is a lookup table that translates Unicode characters drawn from the "Base32 Hex Alphabet" (as specified in Table 4 of RFC 4648) into their 5-bit positive integer equivalents. Characters that are not in the Base32 Hex alphabet but fall within the bounds of the array are translated to -1. -
HEX_ENCODE_TABLE
private static final byte[] HEX_ENCODE_TABLEThis array is a lookup table that translates 5-bit positive integer index values into their "Base 32 Encoding with Extended Hex Alphabet" equivalents as specified in RFC 4648 Section 7, Table 4: Base 32 Encoding with Extended Hex Alphabet. -
MASK_5_BITS
private static final int MASK_5_BITSMask used to extract 5 bits, used when encoding Base32 bytes- See Also:
-
MASK_4_BITS
private static final long MASK_4_BITSMask used to extract 4 bits, used when decoding final trailing character.- See Also:
-
MASK_3_BITS
private static final long MASK_3_BITSMask used to extract 3 bits, used when decoding final trailing character.- See Also:
-
MASK_2_BITS
private static final long MASK_2_BITSMask used to extract 2 bits, used when decoding final trailing character.- See Also:
-
MASK_1_BITS
private static final long MASK_1_BITSMask used to extract 1 bits, used when decoding final trailing character.- See Also:
-
encodeSize
private final int encodeSizeConvenience variable to help us determine when our buffer is going to run out of room and needs resizing.encodeSize = {@link #BYTES_PER_ENCODED_BLOCK} + lineSeparator.length; -
lineSeparator
private final byte[] lineSeparatorLine separator for encoding. Not used when decoding. Only used if lineLength > 0.
-
-
Constructor Details
-
Base32
public Base32()Constructs a Base32 codec used for decoding and encoding.When encoding the line length is 0 (no chunking).
-
Base32
Deprecated.Usebuilder()andBase32.Builder.Constructs a Base32 codec used for decoding and encoding.When encoding the line length is 0 (no chunking).
- Parameters:
useHex-- If true, then use RFC 4648 Section 7, Table 4: Base 32 Encoding with Extended Hex Alphabet
- If false, then use RFC 4648 Section 6, Table 3: The Base 32 Alphabet
-
Base32
Deprecated.Usebuilder()andBase32.Builder.Constructs a Base32 codec used for decoding and encoding.When encoding the line length is 0 (no chunking).
- Parameters:
useHex-- If true, then use RFC 4648 Section 7, Table 4: Base 32 Encoding with Extended Hex Alphabet
- If false, then use RFC 4648 Section 6, Table 3: The Base 32 Alphabet
padding- byte used as padding byte.
-
Base32
-
Base32
Deprecated.Usebuilder()andBase32.Builder.Constructs a Base32 codec used for decoding and encoding.When encoding the line length is 0 (no chunking).
- Parameters:
pad- byte used as padding byte.
-
Base32
Deprecated.Usebuilder()andBase32.Builder.Constructs a Base32 codec used for decoding and encoding.When encoding the line length is given in the constructor, the line separator is CRLF.
- Parameters:
lineLength- Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 8). If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
-
Base32
Deprecated.Usebuilder()andBase32.Builder.Constructs a Base32 codec used for decoding and encoding.When encoding the line length and line separator are given in the constructor.
Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
- Parameters:
lineLength- Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 8). If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.lineSeparator- Each line of encoded data will end with this sequence of bytes.- Throws:
IllegalArgumentException- Thrown when thelineSeparatorcontains Base32 characters.
-
Base32
Deprecated.Usebuilder()andBase32.Builder.Constructs a Base32 / Base32 Hex codec used for decoding and encoding.When encoding the line length and line separator are given in the constructor.
Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
- Parameters:
lineLength- Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 8). If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.lineSeparator- Each line of encoded data will end with this sequence of bytes.useHex-- If true, then use RFC 4648 Section 7, Table 4: Base 32 Encoding with Extended Hex Alphabet
- If false, then use RFC 4648 Section 6, Table 3: The Base 32 Alphabet
- Throws:
IllegalArgumentException- Thrown when thelineSeparatorcontains Base32 characters. Or the lineLength > 0 and lineSeparator is null.
-
Base32
Deprecated.Usebuilder()andBase32.Builder.Constructs a Base32 / Base32 Hex codec used for decoding and encoding.When encoding the line length and line separator are given in the constructor.
Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
- Parameters:
lineLength- Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 8). If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.lineSeparator- Each line of encoded data will end with this sequence of bytes.useHex-- If true, then use RFC 4648 Section 7, Table 4: Base 32 Encoding with Extended Hex Alphabet
- If false, then use RFC 4648 Section 6, Table 3: The Base 32 Alphabet
padding- padding byte.- Throws:
IllegalArgumentException- Thrown when thelineSeparatorcontains Base32 characters. Or the lineLength > 0 and lineSeparator is null.
-
Base32
@Deprecated public Base32(int lineLength, byte[] lineSeparator, boolean useHex, byte padding, CodecPolicy decodingPolicy) Deprecated.Usebuilder()andBase32.Builder.Constructs a Base32 / Base32 Hex codec used for decoding and encoding.When encoding the line length and line separator are given in the constructor.
Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
- Parameters:
lineLength- Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 8). If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.lineSeparator- Each line of encoded data will end with this sequence of bytes.useHex-- If true, then use RFC 4648 Section 7, Table 4: Base 32 Encoding with Extended Hex Alphabet
- If false, then use RFC 4648 Section 6, Table 3: The Base 32 Alphabet
padding- padding byte.decodingPolicy- The decoding policy.- Throws:
IllegalArgumentException- Thrown when thelineSeparatorcontains Base32 characters. Or the lineLength > 0 and lineSeparator is null.- Since:
- 1.15
-
-
Method Details
-
builder
Creates a new Builder.To configure a new instance, use a
Base32.Builder. For example:Base32 base32 = Base32.builder() .setDecodingPolicy(DecodingPolicy.LENIENT) // default is lenient .setLineLength(0) // default is none .setLineSeparator('\r', '\n') // default is CR LF .setPadding('=') // default is '=' .setEncodeTable(customEncodeTable) // default is RFC 4648 Section 6, Table 3: The Base 32 Alphabet .get()- Returns:
- a new Builder.
- Since:
- 1.17.0
-
decodeTable
private static byte[] decodeTable(boolean useHex) -
encodeTable
private static byte[] encodeTable(boolean useHex) Gets the encoding table that matchesuseHex.- Parameters:
useHex-- If true, then use RFC 4648 Section 7, Table 4: Base 32 Encoding with Extended Hex Alphabet
- If false, then use RFC 4648 Section 6, Table 3: The Base 32 Alphabet
- Returns:
- the encoding table that matches
useHex.
-
decode
Decodes all of the provided data, starting at inPos, for inAvail bytes. Should be called at least twice: once with the data to decode, and once with inAvail set to "-1" to alert decoder that EOF has been reached. The "-1" call is not necessary when decoding, but it doesn't hurt, either.
Ignores all non-Base32 characters. This is how chunked (for example 76 character) data is handled, since CR and LF are silently ignored, but has implications for other bytes, too. This method subscribes to the garbage-in, garbage-out philosophy: it will not check the provided data for validity.
Output is written to
Context#bufferas 8-bit octets, usingContext#posas the buffer position- Specified by:
decodein classBaseNCodec- Parameters:
input- byte[] array of ASCII data to Base32 decode.inPos- Position to start reading data from.inAvail- Amount of bytes available from input for decoding.context- the context to be used.
-
encode
Encodes all of the provided data, starting at inPos, for inAvail bytes. Must be called at least twice: once with the data to encode, and once with inAvail set to "-1" to alert encoder that EOF has been reached, so flush last remaining bytes (if not multiple of 5).
- Specified by:
encodein classBaseNCodec- Parameters:
input- byte[] array of binary data to Base32 encode.inPos- Position to start reading data from.inAvail- Amount of bytes available from input for encoding.context- the context to be used.
-
getLineSeparator
byte[] getLineSeparator()Gets the line separator (for testing only).- Returns:
- the line separator.
-
isInAlphabet
public boolean isInAlphabet(byte octet) Returns whether or not theoctetis in the Base32 alphabet.- Specified by:
isInAlphabetin classBaseNCodec- Parameters:
octet- The value to test.- Returns:
trueif the value is defined in the Base32 alphabetfalseotherwise.
-
validateCharacter
Validates whether decoding the final trailing character is possible in the context of the set of possible Base32 values.The character is valid if the lower bits within the provided mask are zero. This is used to test the final trailing base-32 digit is zero in the bits that will be discarded.
- Parameters:
emptyBitsMask- The mask of the lower bits that should be empty.context- the context to be used.- Throws:
IllegalArgumentException- if the bits being checked contain any non-zero value.
-
validateTrailingCharacters
private void validateTrailingCharacters()Validates whether decoding allows final trailing characters that cannot be created during encoding.- Throws:
IllegalArgumentException- if strict decoding is enabled.
-
builder()andBase32.Builder.