Top Description Fields Constructors Methods
org.eclipse.jgit.util

public Class Base64

extends Object
Class Inheritance
Static Imports
java.nio.charset.StandardCharsets.UTF_8

Encodes and decodes to and from Base64 notation.

I am placing this code in the Public Domain. Do with it as you will. This software comes with no guarantees or warranties but with plenty of well-wishing instead! Please visit http://iharder.net/base64 periodically to check for updates or to contribute improvements.

Authors
Robert Harder, [email protected]

Field Summary

Modifier and TypeField and Description
private static final byte[]
DEC

Translates a Base64 value to either its 6-bit reconstruction value or a negative number indicating some other meaning.

private static final byte[]
ENC

The 64 valid Base64 values.

private static final byte
EQUALS_SIGN

The equals sign (=) as a byte.

private static final byte
EQUALS_SIGN_DEC

Indicates equals sign in encoding.

private static final byte
INVALID_DEC

Indicates an invalid byte during decoding.

private static final byte
WHITE_SPACE_DEC

Indicates white space in encoding.

Constructor Summary

AccessConstructor and Description
private
Base64()

Defeats instantiation.

Method Summary

Modifier and TypeMethod and Description
public static byte[]

Returns:

decoded data
decode
(byte[]
The Base64 encoded data
source
,
int
The offset of where to begin decoding
off
,
int
The length of characters to decode
len
)

Low-level decoding ASCII characters from a byte array.

public static byte[]

Returns:

the decoded data
decode
(String
the string to decode
s
)

Decodes data from Base64 notation.

private static int

Returns:

the number of decoded bytes converted
decode4to3
(byte[]
the array to convert
source
,
int
the index where conversion begins
srcOffset
,
byte[]
the array to hold the conversion
destination
,
int
the index where output will be put
destOffset
)

Decodes four bytes from array source and writes the resulting bytes (up to three of them) to destination.

private static void
encode3to4(byte[]
the array to convert
source
,
int
the index where conversion begins
srcOffset
,
int
the number of significant bytes in your array
numSigBytes
,
byte[]
the array to hold the conversion
destination
,
int
the index where output will be put
destOffset
)

Encodes up to three bytes of the array source and writes the resulting four Base64 bytes to destination.

public static String

Returns:

encoded base64 representation of source.
encodeBytes
(byte[]
The data to convert
source
)

Encodes a byte array into Base64 notation.

public static String

Returns:

encoded base64 representation of source.
encodeBytes
(byte[]
The data to convert
source
,
int
Offset in array where conversion should begin
off
,
int
Length of data to convert
len
)

Encodes a byte array into Base64 notation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DECback to summary
private static final byte[] DEC

Translates a Base64 value to either its 6-bit reconstruction value or a negative number indicating some other meaning. The table is only 7 bits wide, as the 8th bit is discarded during decoding.

ENCback to summary
private static final byte[] ENC

The 64 valid Base64 values.

EQUALS_SIGNback to summary
private static final byte EQUALS_SIGN

The equals sign (=) as a byte.

EQUALS_SIGN_DECback to summary
private static final byte EQUALS_SIGN_DEC

Indicates equals sign in encoding.

INVALID_DECback to summary
private static final byte INVALID_DEC

Indicates an invalid byte during decoding.

WHITE_SPACE_DECback to summary
private static final byte WHITE_SPACE_DEC

Indicates white space in encoding.

Constructor Detail

Base64back to summary
private Base64()

Defeats instantiation.

Method Detail

decodeback to summary
public static byte[] decode(byte[] source, int off, int len)

Low-level decoding ASCII characters from a byte array.

Parameters
source:byte[]

The Base64 encoded data

off:int

The offset of where to begin decoding

len:int

The length of characters to decode

Returns:byte[]

decoded data

Exceptions
IllegalArgumentException:
the input is not a valid Base64 sequence.
decodeback to summary
public static byte[] decode(String s)

Decodes data from Base64 notation.

Parameters
s:String

the string to decode

Returns:byte[]

the decoded data

decode4to3back to summary
private static int decode4to3(byte[] source, int srcOffset, byte[] destination, int destOffset)

Decodes four bytes from array source and writes the resulting bytes (up to three of them) to destination. The source and destination arrays can be manipulated anywhere along their length by specifying srcOffset and destOffset. This method does not check to make sure your arrays are large enough to accommodate srcOffset + 4 for the source array or destOffset + 3 for the destination array. This method returns the actual number of bytes that were converted from the Base64 encoding.

Parameters
source:byte[]

the array to convert

srcOffset:int

the index where conversion begins

destination:byte[]

the array to hold the conversion

destOffset:int

the index where output will be put

Returns:int

the number of decoded bytes converted

Annotations
@SuppressWarnings:UnnecessaryParentheses
encode3to4back to summary
private static void encode3to4(byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset)

Encodes up to three bytes of the array source and writes the resulting four Base64 bytes to destination. The source and destination arrays can be manipulated anywhere along their length by specifying srcOffset and destOffset. This method does not check to make sure your arrays are large enough to accommodate srcOffset + 3 for the source array or destOffset + 4 for the destination array. The actual number of significant bytes in your array is given by numSigBytes.

Parameters
source:byte[]

the array to convert

srcOffset:int

the index where conversion begins

numSigBytes:int

the number of significant bytes in your array

destination:byte[]

the array to hold the conversion

destOffset:int

the index where output will be put

Annotations
@SuppressWarnings:UnnecessaryParentheses
encodeBytesback to summary
public static String encodeBytes(byte[] source)

Encodes a byte array into Base64 notation.

Parameters
source:byte[]

The data to convert

Returns:String

encoded base64 representation of source.

encodeBytesback to summary
public static String encodeBytes(byte[] source, int off, int len)

Encodes a byte array into Base64 notation.

Parameters
source:byte[]

The data to convert

off:int

Offset in array where conversion should begin

len:int

Length of data to convert

Returns:String

encoded base64 representation of source.