Package com.unboundid.util
Class Base32
java.lang.Object
com.unboundid.util.Base32
This class provides methods for encoding and decoding data in base32 as
defined in RFC 4648. It
provides a somewhat compact way of representing binary data using only
printable characters (a subset of ASCII letters and numeric digits selected
to avoid ambiguity, like confusion between the number 1 and the uppercase
letter I, and between the number 0 and the uppercase letter O). It uses a
five-bit encoding mechanism in which every five bytes of raw data is
converted into eight bytes of base32-encoded data.
Example
The following examples demonstrate the process for base32-encoding raw data, and for decoding a string containing base32-encoded data back to the raw data used to create it:
// Base32-encode some raw data:
String base32String = Base32.encode(rawDataBytes);
// Decode a base32 string back to raw data:
byte[] decodedRawDataBytes;
try
{
decodedRawDataBytes = Base32.decode(base32String);
}
catch (ParseException pe)
{
// The string did not represent a valid base32 encoding.
decodedRawDataBytes = null;
}
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]Decodes the contents of the provided base32-encoded string.static StringdecodeToString(String data) Decodes the contents of the provided base32-encoded string to a string containing the raw data using the UTF-8 encoding.static Stringencode(byte[] data) Encodes the provided data in base32 format.static voidencode(byte[] data, int off, int length, ByteStringBuffer buffer) Appends a base32-encoded representation of the provided data to the given buffer.static voidencode(byte[] data, int off, int length, StringBuilder buffer) Appends a base32-encoded representation of the provided data to the given buffer.static voidencode(byte[] data, ByteStringBuffer buffer) Appends a base32-encoded representation of the provided data to the given buffer.static voidencode(byte[] data, StringBuilder buffer) Appends a base32-encoded representation of the provided data to the given buffer.static StringEncodes the UTF-8 representation of the provided string in base32 format.static voidencode(String data, ByteStringBuffer buffer) Appends a base32-encoded version of the contents of the provided buffer (using a UTF-8 representation) to the given buffer.static voidencode(String data, StringBuilder buffer) Appends a base32-encoded version of the contents of the provided buffer (using a UTF-8 representation) to the given buffer.
-
Method Details
-
encode
Encodes the UTF-8 representation of the provided string in base32 format.- Parameters:
data- The raw data to be encoded. It must not benull.- Returns:
- The base32-encoded representation of the provided data.
-
encode
Encodes the provided data in base32 format.- Parameters:
data- The raw data to be encoded. It must not benull.- Returns:
- The base32-encoded representation of the provided data.
-
encode
Appends a base32-encoded version of the contents of the provided buffer (using a UTF-8 representation) to the given buffer.- Parameters:
data- The raw data to be encoded. It must not benull.buffer- The buffer to which the base32-encoded data is to be written.
-
encode
Appends a base32-encoded version of the contents of the provided buffer (using a UTF-8 representation) to the given buffer.- Parameters:
data- The raw data to be encoded. It must not benull.buffer- The buffer to which the base32-encoded data is to be written.
-
encode
Appends a base32-encoded representation of the provided data to the given buffer.- Parameters:
data- The raw data to be encoded. It must not benull.buffer- The buffer to which the base32-encoded data is to be written.
-
encode
Appends a base32-encoded representation of the provided data to the given buffer.- Parameters:
data- The array containing the raw data to be encoded. It must not benull.off- The offset in the array at which the data to encode begins.length- The number of bytes to be encoded.buffer- The buffer to which the base32-encoded data is to be written.
-
encode
Appends a base32-encoded representation of the provided data to the given buffer.- Parameters:
data- The raw data to be encoded. It must not benull.buffer- The buffer to which the base32-encoded data is to be written.
-
encode
public static void encode(@NotNull byte[] data, int off, int length, @NotNull ByteStringBuffer buffer) Appends a base32-encoded representation of the provided data to the given buffer.- Parameters:
data- The raw data to be encoded. It must not benull.off- The offset in the array at which the data to encode begins.length- The number of bytes to be encoded.buffer- The buffer to which the base32-encoded data is to be written.
-
decode
Decodes the contents of the provided base32-encoded string.- Parameters:
data- The base32-encoded string to decode. It must not benull.- Returns:
- A byte array containing the decoded data.
- Throws:
ParseException- If the contents of the provided string cannot be parsed as base32-encoded data.
-
decodeToString
Decodes the contents of the provided base32-encoded string to a string containing the raw data using the UTF-8 encoding.- Parameters:
data- The base32-encoded string to decode. It must not benull.- Returns:
- A string containing the decoded data.
- Throws:
ParseException- If the contents of the provided string cannot be parsed as base32-encoded data using the UTF-8 encoding.
-