APIdia Launch Partner
Top Description Inners Constructors Methods
java.util

public final Class Base64

extends Object
Class Inheritance
Imports
java.io.FilterOutputStream, .InputStream, .IOException, .OutputStream, java.nio.ByteBuffer, sun.nio.cs.ISO_8859_1, jdk.internal.access.JavaLangAccess, .SharedSecrets, jdk.internal.util.Preconditions, jdk.internal.vm.annotation.IntrinsicCandidate

This class consists exclusively of static methods for obtaining encoders and decoders for the Base64 encoding scheme. The implementation of this class supports the following types of Base64 as specified in RFC 4648 and RFC 2045.

Unless otherwise noted, passing a null argument to a method of this class will cause a NullPointerException to be thrown.

Author
Xueming Shen
Since
1.8
External Specification
https://www.rfc-editor.org/info/rfc2045, https://www.rfc-editor.org/info/rfc4648

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static class
Base64.Decoder

This class implements a decoder for decoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.

public static class
Base64.Encoder

This class implements an encoder for encoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.

private static class

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Base64.Decoder

Returns:

A Base64 decoder.
getDecoder
()

Returns a Decoder that decodes using the Basic type base64 encoding scheme.

public static Base64.Encoder

Returns:

A Base64 encoder.
getEncoder
()

Returns a Encoder that encodes using the Basic type base64 encoding scheme.

public static Base64.Decoder

Returns:

A Base64 decoder.
getMimeDecoder
()

Returns a Decoder that decodes using the MIME type base64 decoding scheme.

public static Base64.Encoder

Returns:

A Base64 encoder.
getMimeEncoder
()

Returns a Encoder that encodes using the MIME type base64 encoding scheme.

public static Base64.Encoder

Returns:

A Base64 encoder.
getMimeEncoder
(int
the length of each output line (rounded down to nearest multiple of 4). If the rounded down line length is not a positive value, the output will not be separated in lines
lineLength
,
byte[]
the line separator for each output line
lineSeparator
)

Returns a Encoder that encodes using the MIME type base64 encoding scheme with specified line length and line separators.

public static Base64.Decoder

Returns:

A Base64 decoder.
getUrlDecoder
()

Returns a Decoder that decodes using the URL and Filename safe type base64 encoding scheme.

public static Base64.Encoder

Returns:

A Base64 encoder.
getUrlEncoder
()

Returns a Encoder that encodes using the URL and Filename safe type base64 encoding scheme.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Base64back to summary
private Base64()

Method Detail

getDecoderback to summary
public static Base64.Decoder getDecoder()

Returns a Decoder that decodes using the Basic type base64 encoding scheme.

Returns:Base64.Decoder

A Base64 decoder.

getEncoderback to summary
public static Base64.Encoder getEncoder()

Returns a Encoder that encodes using the Basic type base64 encoding scheme.

Returns:Base64.Encoder

A Base64 encoder.

getMimeDecoderback to summary
public static Base64.Decoder getMimeDecoder()

Returns a Decoder that decodes using the MIME type base64 decoding scheme.

Returns:Base64.Decoder

A Base64 decoder.

getMimeEncoderback to summary
public static Base64.Encoder getMimeEncoder()

Returns a Encoder that encodes using the MIME type base64 encoding scheme.

Returns:Base64.Encoder

A Base64 encoder.

getMimeEncoderback to summary
public static Base64.Encoder getMimeEncoder(int lineLength, byte[] lineSeparator)

Returns a Encoder that encodes using the MIME type base64 encoding scheme with specified line length and line separators.

Parameters
lineLength:int

the length of each output line (rounded down to nearest multiple of 4). If the rounded down line length is not a positive value, the output will not be separated in lines

lineSeparator:byte[]

the line separator for each output line

Returns:Base64.Encoder

A Base64 encoder.

Exceptions
IllegalArgumentException:
if lineSeparator includes any character of "The Base64 Alphabet" as specified in Table 1 of RFC 2045.
getUrlDecoderback to summary
public static Base64.Decoder getUrlDecoder()

Returns a Decoder that decodes using the URL and Filename safe type base64 encoding scheme.

Returns:Base64.Decoder

A Base64 decoder.

getUrlEncoderback to summary
public static Base64.Encoder getUrlEncoder()

Returns a Encoder that encodes using the URL and Filename safe type base64 encoding scheme.

Returns:Base64.Encoder

A Base64 encoder.

java.util back to summary

private Class Base64.DecInputStream

extends InputStream
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final int[]
private int
private boolean
private boolean
private final InputStream
private final boolean
private int
private byte[]
private int

Constructor Summary

AccessConstructor and Description
pack-priv
DecInputStream(InputStream is, int[] base64, boolean isMIME)

Method Summary

Modifier and TypeMethod and Description
public int
available()

Overrides java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.

public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.

private int
eof(byte[] b, int off, int pos, int limit)

private int
leftovers(byte[] b, int off, int pos, int limit)

private int
padding(byte[] b, int off, int pos, int limit)

public int
read()

Implements abstract java.io.InputStream.read.

Reads the next byte of data from the input stream.

public int
read(byte[] b, int off, int len)

Overrides java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes.

Inherited from java.io.InputStream:
markmarkSupportednullInputStreamreadreadAllBytesreadNBytesreadNBytesresetskipskipNBytestransferTo

Field Detail

base64back to summary
private final int[] base64
bitsback to summary
private int bits
closedback to summary
private boolean closed
eofback to summary
private boolean eof
isback to summary
private final InputStream is
isMIMEback to summary
private final boolean isMIME
rposback to summary
private int rpos
sbBufback to summary
private byte[] sbBuf
wposback to summary
private int wpos

Constructor Detail

DecInputStreamback to summary
pack-priv DecInputStream(InputStream is, int[] base64, boolean isMIME)

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.InputStream.available.

Doc from java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected. The read might be on the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

A subclass's implementation of this method may choose to throw an IOException if this input stream has been closed by invoking the close() method.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
closeback to summary
public void close() throws IOException

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.InputStream.close.

Closes this input stream and releases any system resources associated with the stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
eofback to summary
private int eof(byte[] b, int off, int pos, int limit) throws IOException
leftoversback to summary
private int leftovers(byte[] b, int off, int pos, int limit)
paddingback to summary
private int padding(byte[] b, int off, int pos, int limit) throws IOException
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Overrides java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

In every case, elements b[0] through b[off-1] and elements b[off+len] through b[b.length-1] are unaffected.

Annotations
@Override
Exceptions
IOException:
If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.
java.util back to summary

public Class Base64.Decoder

extends Object
Class Inheritance

This class implements a decoder for decoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.

The Base64 padding character '=' is accepted and interpreted as the end of the encoded byte data, but is not required. So if the final unit of the encoded byte data only has two or three Base64 characters (without the corresponding padding character(s) padded), they are decoded as if followed by padding character(s). If there is a padding character present in the final unit, the correct number of padding character(s) must be present, otherwise IllegalArgumentException ( IOException when reading from a Base64 stream) is thrown during decoding.

Instances of Decoder class are safe for use by multiple concurrent threads.

Unless otherwise noted, passing a null argument to a method of this class will cause a NullPointerException to be thrown.

If the decoded byte output of the needed size can not be allocated, the decode methods of this class will cause an OutOfMemoryError to be thrown.

Since
1.8
See Also
Encoder

Field Summary

Modifier and TypeField and Description
private static final int[]
fromBase64

Lookup table for decoding unicode characters drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045) into their 6-bit positive integer equivalents.

private static final int[]
fromBase64URL

Lookup table for decoding "URL and Filename safe Base64 Alphabet" as specified in Table2 of the RFC 4648.

private final boolean
private final boolean
pack-priv static final Base64.Decoder
pack-priv static final Base64.Decoder
pack-priv static final Base64.Decoder

Constructor Summary

AccessConstructor and Description
private
Decoder(boolean isURL, boolean isMIME)

Method Summary

Modifier and TypeMethod and Description
public byte[]

Returns:

A newly-allocated byte array containing the decoded bytes.
decode
(byte[]
the byte array to decode
src
)

Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into a newly-allocated output byte array.

public byte[]

Returns:

A newly-allocated byte array containing the decoded bytes.
decode
(String
the string to decode
src
)

Decodes a Base64 encoded String into a newly-allocated byte array using the Base64 encoding scheme.

public int

Returns:

The number of bytes written to the output byte array
decode
(byte[]
the byte array to decode
src
,
byte[]
the output byte array
dst
)

Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into the given output byte array, starting at offset 0.

public ByteBuffer

Returns:

A newly-allocated byte buffer containing the decoded bytes
decode
(ByteBuffer
the ByteBuffer to decode
buffer
)

Decodes all bytes from the input byte buffer using the Base64 encoding scheme, writing the results into a newly-allocated ByteBuffer.

private int
decode0(byte[] src, int sp, int sl, byte[] dst)

private int

Returns:

the number of destination data bytes produced
decodeBlock
(byte[]
the source byte array of Base64 encoded bytes
src
,
int
the offset into src array to begin reading
sp
,
int
the offset (exclusive) past the last byte to be converted.
sl
,
byte[]
the destination byte array of decoded data bytes
dst
,
int
the offset into dst array to begin writing
dp
,
boolean
boolean, when true decode RFC4648 URL-safe base64 characters
isURL
,
boolean
boolean, when true decode according to RFC2045 (ignore illegal chars)
isMIME
)

Decodes base64 characters, and returns the number of data bytes written into the destination array.

private int

Returns:

length of the decoded bytes
decodedOutLength
(byte[]
the byte array to decode
src
,
int
the source position
sp
,
int
the source limit
sl
)

Calculates the length of the decoded output bytes.

public InputStream

Returns:

the input stream for decoding the specified Base64 encoded byte stream
wrap
(InputStream
the input stream
is
)

Returns an input stream for decoding Base64 encoded byte stream.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fromBase64back to summary
private static final int[] fromBase64

Lookup table for decoding unicode characters drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045) into their 6-bit positive integer equivalents. Characters that are not in the Base64 alphabet but fall within the bounds of the array are encoded to -1.

fromBase64URLback to summary
private static final int[] fromBase64URL

Lookup table for decoding "URL and Filename safe Base64 Alphabet" as specified in Table2 of the RFC 4648.

isMIMEback to summary
private final boolean isMIME
isURLback to summary
private final boolean isURL
RFC2045back to summary
pack-priv static final Base64.Decoder RFC2045
RFC4648back to summary
pack-priv static final Base64.Decoder RFC4648
RFC4648_URLSAFEback to summary
pack-priv static final Base64.Decoder RFC4648_URLSAFE

Constructor Detail

Decoderback to summary
private Decoder(boolean isURL, boolean isMIME)

Method Detail

decodeback to summary
public byte[] decode(byte[] src)

Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into a newly-allocated output byte array. The returned byte array is of the length of the resulting bytes.

Parameters
src:byte[]

the byte array to decode

Returns:byte[]

A newly-allocated byte array containing the decoded bytes.

Exceptions
IllegalArgumentException:
if src is not in valid Base64 scheme
decodeback to summary
public byte[] decode(String src)

Decodes a Base64 encoded String into a newly-allocated byte array using the Base64 encoding scheme.

An invocation of this method has exactly the same effect as invoking decode(src.getBytes(StandardCharsets.ISO_8859_1))

Parameters
src:String

the string to decode

Returns:byte[]

A newly-allocated byte array containing the decoded bytes.

Exceptions
IllegalArgumentException:
if src is not in valid Base64 scheme
decodeback to summary
public int decode(byte[] src, byte[] dst)

Decodes all bytes from the input byte array using the Base64 encoding scheme, writing the results into the given output byte array, starting at offset 0.

It is the responsibility of the invoker of this method to make sure the output byte array dst has enough space for decoding all bytes from the input byte array. No bytes will be written to the output byte array if the output byte array is not big enough.

If the input byte array is not in valid Base64 encoding scheme then some bytes may have been written to the output byte array before IllegalargumentException is thrown.

Parameters
src:byte[]

the byte array to decode

dst:byte[]

the output byte array

Returns:int

The number of bytes written to the output byte array

Exceptions
IllegalArgumentException:
if src is not in valid Base64 scheme, or dst does not have enough space for decoding all input bytes.
decodeback to summary
public ByteBuffer decode(ByteBuffer buffer)

Decodes all bytes from the input byte buffer using the Base64 encoding scheme, writing the results into a newly-allocated ByteBuffer.

Upon return, the source buffer's position will be updated to its limit; its limit will not have been changed. The returned output buffer's position will be zero and its limit will be the number of resulting decoded bytes

IllegalArgumentException is thrown if the input buffer is not in valid Base64 encoding scheme. The position of the input buffer will not be advanced in this case.

Parameters
buffer:ByteBuffer

the ByteBuffer to decode

Returns:ByteBuffer

A newly-allocated byte buffer containing the decoded bytes

Exceptions
IllegalArgumentException:
if buffer is not in valid Base64 scheme
decode0back to summary
private int decode0(byte[] src, int sp, int sl, byte[] dst)
decodeBlockback to summary
private int decodeBlock(byte[] src, int sp, int sl, byte[] dst, int dp, boolean isURL, boolean isMIME)

Decodes base64 characters, and returns the number of data bytes written into the destination array. It is the fast path for full 4-byte to 3-byte decoding w/o errors. decodeBlock() can be overridden by an arch-specific intrinsic. decodeBlock can choose to decode all, none, or a variable-sized prefix of the src bytes. This allows the intrinsic to decode in chunks of the src that are of a favorable size for the specific processor it's running on. If any illegal base64 bytes are encountered in src by the intrinsic, the intrinsic must return the actual number of valid data bytes already written to dst. Note that the '=' pad character is treated as an illegal Base64 character by decodeBlock, so it will not process a block of 4 bytes containing pad characters. However, MIME decoding ignores illegal characters, so any intrinsic overriding decodeBlock can choose how to handle illegal characters based on the isMIME parameter. Given the parameters, no length check is possible on dst, so dst is assumed to be large enough to store the decoded bytes.

Parameters
src:byte[]

the source byte array of Base64 encoded bytes

sp:int

the offset into src array to begin reading

sl:int

the offset (exclusive) past the last byte to be converted.

dst:byte[]

the destination byte array of decoded data bytes

dp:int

the offset into dst array to begin writing

isURL:boolean

boolean, when true decode RFC4648 URL-safe base64 characters

isMIME:boolean

boolean, when true decode according to RFC2045 (ignore illegal chars)

Returns:int

the number of destination data bytes produced

Annotations
@IntrinsicCandidate
decodedOutLengthback to summary
private int decodedOutLength(byte[] src, int sp, int sl)

Calculates the length of the decoded output bytes.

Parameters
src:byte[]

the byte array to decode

sp:int

the source position

sl:int

the source limit

Returns:int

length of the decoded bytes

wrapback to summary
public InputStream wrap(InputStream is)

Returns an input stream for decoding Base64 encoded byte stream.

The read methods of the returned InputStream will throw IOException when reading bytes that cannot be decoded.

Closing the returned input stream will close the underlying input stream.

Parameters
is:InputStream

the input stream

Returns:InputStream

the input stream for decoding the specified Base64 encoded byte stream

java.util back to summary

public Class Base64.Encoder

extends Object
Class Inheritance

This class implements an encoder for encoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.

Instances of Encoder class are safe for use by multiple concurrent threads.

Unless otherwise noted, passing a null argument to a method of this class will cause a NullPointerException to be thrown.

If the encoded byte output of the needed size can not be allocated, the encode methods of this class will cause an OutOfMemoryError to be thrown.

Since
1.8
See Also
Decoder

Field Summary

Modifier and TypeField and Description
private static final byte[]
private final boolean
private final boolean
private static final JavaLangAccess
private final int
private static final int
private final byte[]
pack-priv static final Base64.Encoder
pack-priv static final Base64.Encoder
pack-priv static final Base64.Encoder
private static final char[]
toBase64

This array is a lookup table that translates 6-bit positive integer index values into their "Base64 Alphabet" equivalents as specified in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648).

private static final char[]
toBase64URL

It's the lookup table for "URL and Filename safe Base64" as specified in Table 2 of the RFC 4648, with the '+' and '/' changed to '-' and '_'.

Constructor Summary

AccessConstructor and Description
private
Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding)

Method Summary

Modifier and TypeMethod and Description
public byte[]

Returns:

A newly-allocated byte array containing the resulting encoded bytes.
encode
(byte[]
the byte array to encode
src
)

Encodes all bytes from the specified byte array into a newly-allocated byte array using the Base64 encoding scheme.

public int

Returns:

The number of bytes written to the output byte array
encode
(byte[]
the byte array to encode
src
,
byte[]
the output byte array
dst
)

Encodes all bytes from the specified byte array using the Base64 encoding scheme, writing the resulting bytes to the given output byte array, starting at offset 0.

public ByteBuffer

Returns:

A newly-allocated byte buffer containing the encoded bytes.
encode
(ByteBuffer
the source ByteBuffer to encode
buffer
)

Encodes all remaining bytes from the specified byte buffer into a newly-allocated ByteBuffer using the Base64 encoding scheme.

private int
encode0(byte[] src, int off, int end, byte[] dst)

private void
encodeBlock(byte[] src, int sp, int sl, byte[] dst, int dp, boolean isURL)

private final int

Returns:

length of the encoded bytes, or -1 if the length overflows
encodedOutLength
(int
length of the bytes to encode
srclen
,
boolean
if true, throws OutOfMemoryError if the length of the encoded bytes overflows; else returns the length
throwOOME
)

Calculates the length of the encoded output bytes.

public String

Returns:

A String containing the resulting Base64 encoded characters
encodeToString
(byte[]
the byte array to encode
src
)

Encodes the specified byte array into a String using the Base64 encoding scheme.

public Base64.Encoder

Returns:

an equivalent encoder that encodes without adding any padding character at the end
withoutPadding
()

Returns an encoder instance that encodes equivalently to this one, but without adding any padding character at the end of the encoded byte data.

public OutputStream

Returns:

the output stream for encoding the byte data into the specified Base64 encoded format
wrap
(OutputStream
the output stream.
os
)

Wraps an output stream for encoding byte data using the Base64 encoding scheme.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CRLFback to summary
private static final byte[] CRLF
doPaddingback to summary
private final boolean doPadding
isURLback to summary
private final boolean isURL
JLAback to summary
private static final JavaLangAccess JLA
linemaxback to summary
private final int linemax
MIMELINEMAXback to summary
private static final int MIMELINEMAX
newlineback to summary
private final byte[] newline
RFC2045back to summary
pack-priv static final Base64.Encoder RFC2045
RFC4648back to summary
pack-priv static final Base64.Encoder RFC4648
RFC4648_URLSAFEback to summary
pack-priv static final Base64.Encoder RFC4648_URLSAFE
toBase64back to summary
private static final char[] toBase64

This array is a lookup table that translates 6-bit positive integer index values into their "Base64 Alphabet" equivalents as specified in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648).

toBase64URLback to summary
private static final char[] toBase64URL

It's the lookup table for "URL and Filename safe Base64" as specified in Table 2 of the RFC 4648, with the '+' and '/' changed to '-' and '_'. This table is used when BASE64_URL is specified.

Constructor Detail

Encoderback to summary
private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding)

Method Detail

encodeback to summary
public byte[] encode(byte[] src)

Encodes all bytes from the specified byte array into a newly-allocated byte array using the Base64 encoding scheme. The returned byte array is of the length of the resulting bytes.

Parameters
src:byte[]

the byte array to encode

Returns:byte[]

A newly-allocated byte array containing the resulting encoded bytes.

encodeback to summary
public int encode(byte[] src, byte[] dst)

Encodes all bytes from the specified byte array using the Base64 encoding scheme, writing the resulting bytes to the given output byte array, starting at offset 0.

It is the responsibility of the invoker of this method to make sure the output byte array dst has enough space for encoding all bytes from the input byte array. No bytes will be written to the output byte array if the output byte array is not big enough.

Parameters
src:byte[]

the byte array to encode

dst:byte[]

the output byte array

Returns:int

The number of bytes written to the output byte array

Exceptions
IllegalArgumentException:
if dst does not have enough space for encoding all input bytes.
encodeback to summary
public ByteBuffer encode(ByteBuffer buffer)

Encodes all remaining bytes from the specified byte buffer into a newly-allocated ByteBuffer using the Base64 encoding scheme. Upon return, the source buffer's position will be updated to its limit; its limit will not have been changed. The returned output buffer's position will be zero and its limit will be the number of resulting encoded bytes.

Parameters
buffer:ByteBuffer

the source ByteBuffer to encode

Returns:ByteBuffer

A newly-allocated byte buffer containing the encoded bytes.

encode0back to summary
private int encode0(byte[] src, int off, int end, byte[] dst)
encodeBlockback to summary
private void encodeBlock(byte[] src, int sp, int sl, byte[] dst, int dp, boolean isURL)
Annotations
@IntrinsicCandidate
encodedOutLengthback to summary
private final int encodedOutLength(int srclen, boolean throwOOME)

Calculates the length of the encoded output bytes.

Parameters
srclen:int

length of the bytes to encode

throwOOME:boolean

if true, throws OutOfMemoryError if the length of the encoded bytes overflows; else returns the length

Returns:int

length of the encoded bytes, or -1 if the length overflows

encodeToStringback to summary
public String encodeToString(byte[] src)

Encodes the specified byte array into a String using the Base64 encoding scheme.

This method first encodes all input bytes into a base64 encoded byte array and then constructs a new String by using the encoded byte array and the ISO-8859-1 charset.

In other words, an invocation of this method has exactly the same effect as invoking new String(encode(src), StandardCharsets.ISO_8859_1).

Parameters
src:byte[]

the byte array to encode

Returns:String

A String containing the resulting Base64 encoded characters

withoutPaddingback to summary
public Base64.Encoder withoutPadding()

Returns an encoder instance that encodes equivalently to this one, but without adding any padding character at the end of the encoded byte data.

The encoding scheme of this encoder instance is unaffected by this invocation. The returned encoder instance should be used for non-padding encoding operation.

Returns:Base64.Encoder

an equivalent encoder that encodes without adding any padding character at the end

wrapback to summary
public OutputStream wrap(OutputStream os)

Wraps an output stream for encoding byte data using the Base64 encoding scheme.

It is recommended to promptly close the returned output stream after use, during which it will flush all possible leftover bytes to the underlying output stream. Closing the returned output stream will close the underlying output stream.

Parameters
os:OutputStream

the output stream.

Returns:OutputStream

the output stream for encoding the byte data into the specified Base64 encoded format

java.util back to summary

private Class Base64.EncOutputStream

extends FilterOutputStream
Class Inheritance

Field Summary

Modifier and TypeField and Description
private int
private int
private int
private final char[]
private byte[]
private boolean
private final boolean
private int
private final int
private int
private final byte[]
Inherited from java.io.FilterOutputStream:
out

Constructor Summary

AccessConstructor and Description
pack-priv
EncOutputStream(OutputStream os, char[] base64, byte[] newline, int linemax, boolean doPadding)

Method Summary

Modifier and TypeMethod and Description
private void
public void
close()

Overrides java.io.FilterOutputStream.close.

Implements java.io.Closeable.close.

Closes this output stream and releases any system resources associated with the stream.

public void
write(int b)

Overrides java.io.FilterOutputStream.write.

Writes the specified byte to this output stream.

public void
write(byte[] b, int off, int len)

Overrides java.io.FilterOutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.

private void
writeb4(char b1, char b2, char b3, char b4)

Inherited from java.io.FilterOutputStream:
flushwrite

Field Detail

b0back to summary
private int b0
b1back to summary
private int b1
b2back to summary
private int b2
base64back to summary
private final char[] base64
bufback to summary
private byte[] buf
closedback to summary
private boolean closed

Hides java.io.FilterOutputStream.closed.

doPaddingback to summary
private final boolean doPadding
leftoverback to summary
private int leftover
linemaxback to summary
private final int linemax
lineposback to summary
private int linepos
newlineback to summary
private final byte[] newline

Constructor Detail

EncOutputStreamback to summary
pack-priv EncOutputStream(OutputStream os, char[] base64, byte[] newline, int linemax, boolean doPadding)

Method Detail

checkNewlineback to summary
private void checkNewline() throws IOException
closeback to summary
public void close() throws IOException

Overrides java.io.FilterOutputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.FilterOutputStream.close.

Closes this output stream and releases any system resources associated with the stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(int b) throws IOException

Overrides java.io.FilterOutputStream.write.

Doc from java.io.FilterOutputStream.write.

Writes the specified byte to this output stream.

The write method of FilterOutputStream calls the write method of its underlying output stream, that is, it performs out.write(b).

Implements the abstract write method of OutputStream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(byte[] b, int off, int len) throws IOException

Overrides java.io.FilterOutputStream.write.

Doc from java.io.FilterOutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeb4back to summary
private void writeb4(char b1, char b2, char b3, char b4) throws IOException