Class Base64
- java.lang.Object
-
- com.alibaba.nacos.client.identify.Base64
-
public class Base64 extends Object
Provides Base64 encoding and decoding as defined by RFC 2045.This class implements section 6.8. Base64 Content-Transfer-Encoding from RFC 2045 Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies by Freed and Borenstein.
The class can be parameterized in the following manner with various constructors:
- URL-safe mode: Default off.
- Line length: Default 76. Line length that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
- Line separator: Default is CRLF ("\r\n")
Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
This class is not thread-safe. Each thread should use its own instance.
- Since:
- 1.0
- Version:
- $Revision: 1080712 $
- Author:
- Apache Software Foundation
- See Also:
- RFC 2045
-
-
Constructor Summary
Constructors Constructor Description Base64()Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.Base64(int lineLength, byte[] lineSeparator, boolean urlSafe)Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static byte[]decodeBase64(byte[] base64Data)Decodes Base64 data into octetsstatic byte[]encodeBase64(byte[] binaryData)Encodes binary data using the base64 algorithm but does not chunk the output.static byte[]encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe, int maxResultSize)Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.protected booleanisInAlphabet(byte octet)Returns whether or not theoctetis in the Base32 alphabet.
-
-
-
Constructor Detail
-
Base64
public Base64()
Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.When encoding the line length is 0 (no chunking), and the encoding table is STANDARD_ENCODE_TABLE.
When decoding all variants are supported.
-
Base64
public Base64(int lineLength, byte[] lineSeparator, boolean urlSafe)Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.When encoding the line length and line separator are given in the constructor, and the encoding table is STANDARD_ENCODE_TABLE.
Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
When decoding all variants are supported.
- Parameters:
lineLength- Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 4). 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.urlSafe- Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is only applied to encode operations. Decoding seamlessly handles both modes.- Throws:
IllegalArgumentException- The provided lineSeparator included some base64 characters. That's not going to work!- Since:
- 1.4
-
-
Method Detail
-
encodeBase64
public static byte[] encodeBase64(byte[] binaryData)
Encodes binary data using the base64 algorithm but does not chunk the output.- Parameters:
binaryData- binary data to encode- Returns:
- byte[] containing Base64 characters in their UTF-8 representation.
-
encodeBase64
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe, int maxResultSize)Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.- Parameters:
binaryData- Array containing binary data to encode.isChunked- iftruethis encoder will chunk the base64 output into 76 character blocksurlSafe- iftruethis encoder will emit - and _ instead of the usual + and / characters.maxResultSize- The maximum result size to accept.- Returns:
- Base64-encoded data.
- Throws:
IllegalArgumentException- Thrown when the input array needs an output array bigger than maxResultSize- Since:
- 1.4
-
decodeBase64
public static byte[] decodeBase64(byte[] base64Data)
Decodes Base64 data into octets- Parameters:
base64Data- Byte array containing Base64 data- Returns:
- Array containing decoded data.
-
isInAlphabet
protected boolean isInAlphabet(byte octet)
Returns whether or not theoctetis in the Base32 alphabet.- Parameters:
octet- The value to test- Returns:
trueif the value is defined in the the Base32 alphabetfalseotherwise.
-
-