Package org.forgerock.util.encode
Class Base64
- java.lang.Object
-
- org.forgerock.util.encode.Base64
-
public final class Base64 extends Object
Provides RFC 4648 / RFC 2045 compatible Base64 encoding and decoding. The implementation is based onBase64
. The normal methods use the MIME decoder for compatibility with the permissive decoding of the previous implementation. All invalid characters and newlines in the input are ignored. Use the newdecodeStrict(byte[])
ordecodeStrict(String)
methods if strict decoding is preferred, for example in security-sensitive uses.
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static byte[]
decode(byte[] sArr)
Decodes a BASE64 encoded byte array.static byte[]
decode(char[] sArr)
Decodes a BASE64 encoded char array.static byte[]
decode(String str)
Decodes a BASE64 encodedString
.static byte[]
decodeFast(byte[] sArr)
Deprecated.Usedecode(byte[])
instead.static byte[]
decodeFast(char[] sArr)
Deprecated.Usedecode(char[])
instead.static byte[]
decodeFast(String s)
Deprecated.Usedecode(String)
instead.static byte[]
decodeStrict(byte[] encoded)
Decodes the input using a strict decoder that rejects any invalid characters or line separators.static byte[]
decodeStrict(char[] encoded)
Decodes the input using a strict decoder that rejects any invalid characters or line separators.static byte[]
decodeStrict(String encoded)
Decodes the input using a strict decoder that rejects any invalid characters or line separators.static String
encode(byte[] content)
This method is usingencode(byte[], boolean)
, and it only exists so we don't break the API.static String
encode(byte[] data, boolean lineSep)
Encodes a raw byte array into a BASE64String
representation i accordance with RFC 2045.static byte[]
encodeToByte(byte[] data, boolean lineSep)
Encodes a raw byte array into a BASE64byte[]
representation i accordance with RFC 2045.static char[]
encodeToChar(byte[] sArr, boolean lineSep)
Encodes a raw byte array into a BASE64char[]
representation i accordance with RFC 2045.
-
-
-
Method Detail
-
decode
public static byte[] decode(byte[] sArr)
Decodes a BASE64 encoded byte array. All illegal characters will be ignored and can handle both arrays with and without line separators. UsedecodeStrict(byte[])
if rejection of invalid characters is required.- Parameters:
sArr
- The source array. Length 0 will return an empty array.null
will throw an exception. Returnsnull
if the padding is incorrect.- Returns:
- The decoded array of bytes. May be of length 0.
-
decode
public static byte[] decode(char[] sArr)
Decodes a BASE64 encoded char array. All illegal characters will be ignored and can handle both arrays with and without line separators. UsedecodeStrict(char[])
if rejection of invalid characters is required.- Parameters:
sArr
- The source array.null
or length 0 will return an empty array.- Returns:
- The decoded array of bytes. May be of length 0. Returns
null
if the padding is incorrect.
-
decode
public static byte[] decode(String str)
Decodes a BASE64 encodedString
. All illegal characters will be ignored and can handle both strings with and without line separators. UsedecodeStrict(String)
if rejection of invalid characters is required.- Parameters:
str
- The source string.null
or length 0 will return an empty array.- Returns:
- The decoded array of bytes. May be of length 0. Returns
null
if the padding is incorrect.
-
decodeFast
@Deprecated public static byte[] decodeFast(byte[] sArr)
Deprecated.Usedecode(byte[])
instead.This method is now a direct synonym fordecode(byte[])
.- Parameters:
sArr
- The source array. Length 0 will return an empty array.null
will throw an exception.- Returns:
- The decoded array of bytes. May be of length 0.
-
decodeFast
@Deprecated public static byte[] decodeFast(char[] sArr)
Deprecated.Usedecode(char[])
instead.This method is now a direct synonym fordecode(char[])
.- Parameters:
sArr
- The source array. Length 0 will return an empty array.null
will throw an exception.- Returns:
- The decoded array of bytes. May be of length 0.
-
decodeFast
@Deprecated public static byte[] decodeFast(String s)
Deprecated.Usedecode(String)
instead.This method is now a direct synonym fordecode(String)
.- Parameters:
s
- The source string. Length 0 will return an empty array.null
will throw an exception.- Returns:
- The decoded array of bytes. May be of length 0.
-
decodeStrict
public static byte[] decodeStrict(byte[] encoded)
Decodes the input using a strict decoder that rejects any invalid characters or line separators.- Parameters:
encoded
- the encoded data.- Returns:
- the decoded data.
- Throws:
IllegalArgumentException
- if the data is malformed in any way.
-
decodeStrict
public static byte[] decodeStrict(char[] encoded)
Decodes the input using a strict decoder that rejects any invalid characters or line separators.- Parameters:
encoded
- the encoded data.- Returns:
- the decoded data.
- Throws:
IllegalArgumentException
- if the data is malformed in any way.
-
decodeStrict
public static byte[] decodeStrict(String encoded)
Decodes the input using a strict decoder that rejects any invalid characters or line separators.- Parameters:
encoded
- the encoded data.- Returns:
- the decoded data.
- Throws:
IllegalArgumentException
- if the data is malformed in any way.
-
encode
public static String encode(byte[] content)
This method is usingencode(byte[], boolean)
, and it only exists so we don't break the API.- Parameters:
content
- The bytearray that needs to be Base64 encoded- Returns:
- the Base64 encoded
-
encode
public static String encode(byte[] data, boolean lineSep)
Encodes a raw byte array into a BASE64String
representation i accordance with RFC 2045.- Parameters:
data
- The bytes to convert. Ifnull
or length 0 an empty string will be returned.lineSep
- Optional "\r\n" after 76 characters, unless end of file.
No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a little faster.- Returns:
- A BASE64 encoded array. Never
null
.
-
encodeToByte
public static byte[] encodeToByte(byte[] data, boolean lineSep)
Encodes a raw byte array into a BASE64byte[]
representation i accordance with RFC 2045.- Parameters:
data
- The bytes to convert. Ifnull
or length 0 an empty array will be returned.lineSep
- Optional "\r\n" after 76 characters, unless end of file.
No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a little faster.- Returns:
- A BASE64 encoded array. Never
null
.
-
encodeToChar
public static char[] encodeToChar(byte[] sArr, boolean lineSep)
Encodes a raw byte array into a BASE64char[]
representation i accordance with RFC 2045.- Parameters:
sArr
- The bytes to convert. Ifnull
or length 0 an empty array will be returned.lineSep
- Optional "\r\n" after 76 characters, unless end of file.
No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a little faster.- Returns:
- A BASE64 encoded array. Never
null
.
-
-