Package com.landawn.abacus.util
Class Base64
java.lang.Object
com.landawn.abacus.util.Base64
Note: this file contains the source codes and docs copied from
Apache commons-codec
under Apache License v2 and may be modified.-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
decodeBase64
(byte[] base64Data) Decodes Base64 data into octets.static byte[]
decodeBase64
(String base64String) Decodes a Base64 String into octets.static byte[]
decodeBase64URL
(byte[] base64Data) Decodes Base64 data into octets.static byte[]
decodeBase64URL
(String base64String) Decode base 64 URL.static BigInteger
decodeInteger
(byte[] pArray) Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature.static byte[]
encodeBase64
(byte[] binaryData) Encodes binary data using the base64 algorithm but does not chunk the output.static byte[]
encodeBase64
(byte[] binaryData, boolean isChunked) Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.static byte[]
encodeBase64
(byte[] binaryData, boolean isChunked, boolean urlSafe) Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.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.static byte[]
encodeBase64Chunked
(byte[] binaryData) Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks.static String
encodeBase64String
(byte[] binaryData) Encodes binary data using the base64 algorithm but does not chunk the output.static byte[]
encodeBase64URLSafe
(byte[] binaryData) Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.static String
encodeBase64URLSafeString
(byte[] binaryData) Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.static byte[]
encodeInteger
(BigInteger bigInt) Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature.static boolean
isBase64
(byte octet) Returns whether or not theoctet
is in the base 64 alphabet.static boolean
isBase64
(byte[] arrayOctet) Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.static boolean
Tests a given String to see if it contains only valid characters within the Base64 alphabet.
-
Method Details
-
isBase64
public static boolean isBase64(byte octet) Returns whether or not theoctet
is in the base 64 alphabet.- Parameters:
octet
- The value to test- Returns:
true
if the value is defined in the the base 64 alphabet,false
otherwise.- Since:
- 1.4
-
isBase64
Tests a given String to see if it contains only valid characters within the Base64 alphabet. Currently the method treats whitespace as valid.- Parameters:
base64
- String to test- Returns:
true
if all characters in the String are valid characters in the Base64 alphabet or if the String is empty;false
, otherwise- Since:
- 1.5
-
isBase64
public static boolean isBase64(byte[] arrayOctet) Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the method treats whitespace as valid.- Parameters:
arrayOctet
- byte array to test- Returns:
true
if all bytes are valid characters in the Base64 alphabet or if the byte array is empty;false
, otherwise- Since:
- 1.5
-
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.
-
encodeBase64String
Encodes binary data using the base64 algorithm but does not chunk the output. NOTE: We changed the behaviour of this method from multi-line chunking (commons-codec-1.4) to single-line non-chunking (commons-codec-1.5).- Parameters:
binaryData
- binary data to encode- Returns:
- String containing Base64 characters.
- Since:
- 1.4 (NOTE: 1.4 chunked the output, whereas 1.5 does not).
-
encodeBase64URLSafe
public static byte[] encodeBase64URLSafe(byte[] binaryData) Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of + and / characters. Note: no padding is added.- Parameters:
binaryData
- binary data to encode- Returns:
- byte[] containing Base64 characters in their UTF-8 representation.
- Since:
- 1.4
-
encodeBase64URLSafeString
Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of + and / characters. Note: no padding is added.- Parameters:
binaryData
- binary data to encode- Returns:
- String containing Base64 characters
- Since:
- 1.4
-
encodeBase64Chunked
public static byte[] encodeBase64Chunked(byte[] binaryData) Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks.- Parameters:
binaryData
- binary data to encode- Returns:
- Base64 characters chunked in 76 character blocks
-
encodeBase64
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.- Parameters:
binaryData
- Array containing binary data to encode.isChunked
- iftrue
this encoder will chunk the base64 output into 76 character blocks- Returns:
- Base64-encoded data.
- Throws:
IllegalArgumentException
- Thrown when the input array needs an output array bigger thanInteger.MAX_VALUE
-
encodeBase64
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe) Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.- Parameters:
binaryData
- Array containing binary data to encode.isChunked
- iftrue
this encoder will chunk the base64 output into 76 character blocksurlSafe
- iftrue
this encoder will emit - and _ instead of the usual + and / characters. Note: no padding is added when encoding using the URL-safe alphabet.- Returns:
- Base64-encoded data.
- Throws:
IllegalArgumentException
- Thrown when the input array needs an output array bigger thanInteger.MAX_VALUE
- Since:
- 1.4
-
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
- iftrue
this encoder will chunk the base64 output into 76 character blocksurlSafe
- iftrue
this encoder will emit - and _ instead of the usual + and / characters. Note: no padding is added when encoding using the URL-safe alphabet.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
Decodes a Base64 String into octets.Note: this method seamlessly handles data encoded in URL-safe or normal mode.
- Parameters:
base64String
- String containing Base64 data- Returns:
- Array containing decoded data.
- Since:
- 1.4
-
decodeBase64
public static byte[] decodeBase64(byte[] base64Data) Decodes Base64 data into octets.Note: this method seamlessly handles data encoded in URL-safe or normal mode.
- Parameters:
base64Data
- Byte array containing Base64 data- Returns:
- Array containing decoded data.
-
decodeBase64URL
Decode base 64 URL.- Parameters:
base64String
-- Returns:
-
decodeBase64URL
public static byte[] decodeBase64URL(byte[] base64Data) Decodes Base64 data into octets.Note: this method seamlessly handles data encoded in URL-safe or normal mode.
- Parameters:
base64Data
- Byte array containing Base64 data- Returns:
- Array containing decoded data.
-
decodeInteger
Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature.- Parameters:
pArray
- a byte array containing base64 character data- Returns:
- A BigInteger
- Since:
- 1.4
-
encodeInteger
Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature.- Parameters:
bigInt
- a BigInteger- Returns:
- A byte array containing base64 character data
- Throws:
NullPointerException
- if null is passed in- Since:
- 1.4
-