Class Base32
- java.lang.Object
-
- org.apache.commons.codec.binary.BaseNCodec
-
- org.apache.commons.codec.binary.Base32
-
- All Implemented Interfaces:
BinaryDecoder,BinaryEncoder,Decoder,Encoder
public class Base32 extends BaseNCodec
Provides Base32 encoding and decoding as defined by RFC 4648.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.
- Since:
- 1.5
- See Also:
- RFC 4648
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.commons.codec.binary.BaseNCodec
BaseNCodec.Context
-
-
Field Summary
Fields Modifier and Type Field Description private static intBITS_PER_ENCODED_BYTEBASE32 characters are 5 bits in length.private static intBYTES_PER_ENCODED_BLOCKprivate static intBYTES_PER_UNENCODED_BLOCKprivate static byte[]CHUNK_SEPARATORChunk separator per RFC 2045 section 2.1.private static 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.private intdecodeSizeConvenience variable to help us determine when our buffer is going to run out of room and needs resizing.private byte[]decodeTableDecode table to use.private static byte[]ENCODE_TABLEThis array is a lookup table that translates 5-bit positive integer index values into their "Base32 Alphabet" equivalents as specified in Table 3 of RFC 4648.private intencodeSizeConvenience variable to help us determine when our buffer is going to run out of room and needs resizing.private byte[]encodeTableEncode table to use.private static 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.private static byte[]HEX_ENCODE_TABLEThis array is a lookup table that translates 5-bit positive integer index values into their "Base32 Hex Alphabet" equivalents as specified in Table 4 of RFC 4648.private byte[]lineSeparatorLine separator for encoding.private static intMASK_5BITSMask used to extract 5 bits, used when encoding Base32 bytes-
Fields inherited from class org.apache.commons.codec.binary.BaseNCodec
EOF, lineLength, MASK_8BITS, MIME_CHUNK_SIZE, pad, PAD, PAD_DEFAULT, PEM_CHUNK_SIZE
-
-
Constructor Summary
Constructors Constructor Description Base32()Creates a Base32 codec used for decoding and encoding.Base32(boolean useHex)Creates a Base32 codec used for decoding and encoding.Base32(boolean useHex, byte pad)Creates a Base32 codec used for decoding and encoding.Base32(byte pad)Creates a Base32 codec used for decoding and encoding.Base32(int lineLength)Creates a Base32 codec used for decoding and encoding.Base32(int lineLength, byte[] lineSeparator)Creates a Base32 codec used for decoding and encoding.Base32(int lineLength, byte[] lineSeparator, boolean useHex)Creates a Base32 / Base32 Hex codec used for decoding and encoding.Base32(int lineLength, byte[] lineSeparator, boolean useHex, byte pad)Creates a Base32 / Base32 Hex codec used for decoding and encoding.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voiddecode(byte[] in, int inPos, int inAvail, BaseNCodec.Context context)Decodes all of the provided data, starting at inPos, for inAvail bytes.(package private) voidencode(byte[] in, int inPos, int inAvail, BaseNCodec.Context context)Encodes all of the provided data, starting at inPos, for inAvail bytes.booleanisInAlphabet(byte octet)Returns whether or not theoctetis in the Base32 alphabet.private voidvalidateCharacter(int numBits, BaseNCodec.Context context)Validates whether the character is possible in the context of the set of possible base 32 values.-
Methods inherited from class org.apache.commons.codec.binary.BaseNCodec
available, containsAlphabetOrPad, decode, decode, decode, encode, encode, encode, encodeAsString, encodeToString, ensureBufferSize, getDefaultBufferSize, getEncodedLength, hasData, isInAlphabet, isInAlphabet, isWhiteSpace, readResults
-
-
-
-
Field Detail
-
BITS_PER_ENCODED_BYTE
private static final int BITS_PER_ENCODED_BYTE
BASE32 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:
- Constant Field Values
-
BYTES_PER_ENCODED_BLOCK
private static final int BYTES_PER_ENCODED_BLOCK
- See Also:
- Constant Field Values
-
BYTES_PER_UNENCODED_BLOCK
private static final int BYTES_PER_UNENCODED_BLOCK
- See Also:
- Constant Field Values
-
CHUNK_SEPARATOR
private static final byte[] CHUNK_SEPARATOR
Chunk separator per RFC 2045 section 2.1.- See Also:
- RFC 2045 section 2.1
-
DECODE_TABLE
private static final byte[] DECODE_TABLE
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. 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_TABLE
This array is a lookup table that translates 5-bit positive integer index values into their "Base32 Alphabet" equivalents as specified in Table 3 of RFC 4648.
-
HEX_DECODE_TABLE
private static final byte[] HEX_DECODE_TABLE
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. 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_TABLE
This array is a lookup table that translates 5-bit positive integer index values into their "Base32 Hex Alphabet" equivalents as specified in Table 4 of RFC 4648.
-
MASK_5BITS
private static final int MASK_5BITS
Mask used to extract 5 bits, used when encoding Base32 bytes- See Also:
- Constant Field Values
-
decodeSize
private final int decodeSize
Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.decodeSize =BYTES_PER_ENCODED_BLOCK- 1 + lineSeparator.length;
-
decodeTable
private final byte[] decodeTable
Decode table to use.
-
encodeSize
private final int encodeSize
Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.encodeSize =BYTES_PER_ENCODED_BLOCK+ lineSeparator.length;
-
encodeTable
private final byte[] encodeTable
Encode table to use.
-
lineSeparator
private final byte[] lineSeparator
Line separator for encoding. Not used when decoding. Only used if lineLength > 0.
-
-
Constructor Detail
-
Base32
public Base32()
Creates a Base32 codec used for decoding and encoding.When encoding the line length is 0 (no chunking).
-
Base32
public Base32(byte pad)
Creates 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
public Base32(boolean useHex)
Creates a Base32 codec used for decoding and encoding.When encoding the line length is 0 (no chunking).
- Parameters:
useHex- iftruethen use Base32 Hex alphabet
-
Base32
public Base32(boolean useHex, byte pad)Creates a Base32 codec used for decoding and encoding.When encoding the line length is 0 (no chunking).
- Parameters:
useHex- iftruethen use Base32 Hex alphabetpad- byte used as padding byte.
-
Base32
public Base32(int lineLength)
Creates 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 nearest multiple of 8). If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
-
Base32
public Base32(int lineLength, byte[] lineSeparator)Creates 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 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:
java.lang.IllegalArgumentException- The provided lineSeparator included some Base32 characters. That's not going to work!
-
Base32
public Base32(int lineLength, byte[] lineSeparator, boolean useHex)Creates 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 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- iftrue, then use Base32 Hex alphabet, otherwise use Base32 alphabet- Throws:
java.lang.IllegalArgumentException- The provided lineSeparator included some Base32 characters. That's not going to work! Or the lineLength > 0 and lineSeparator is null.
-
Base32
public Base32(int lineLength, byte[] lineSeparator, boolean useHex, byte pad)Creates 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 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- iftrue, then use Base32 Hex alphabet, otherwise use Base32 alphabetpad- byte used as padding byte.- Throws:
java.lang.IllegalArgumentException- The provided lineSeparator included some Base32 characters. That's not going to work! Or the lineLength > 0 and lineSeparator is null.
-
-
Method Detail
-
decode
void decode(byte[] in, int inPos, int inAvail, BaseNCodec.Context context)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 (e.g. 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.
- Specified by:
decodein classBaseNCodec- Parameters:
in- 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 Output is written toBaseNCodec.Context.bufferas 8-bit octets, usingBaseNCodec.Context.posas the buffer position
-
encode
void encode(byte[] in, int inPos, int inAvail, BaseNCodec.Context context)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:
in- 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
-
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 the Base32 alphabetfalseotherwise.
-
validateCharacter
private void validateCharacter(int numBits, BaseNCodec.Context context)Validates whether the character is possible in the context of the set of possible base 32 values.
- Parameters:
numBits- number of least significant bits to checkcontext- the context to be used- Throws:
java.lang.IllegalArgumentException- if the bits being checked contain any non-zero value
-
-