Spec-Zone .ru
спецификации, руководства, описания, API
|
public static class Base64.Encoder extends Object
Instances of Base64.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.
Base64.Decoder
Modifier and Type | Method and Description |
---|---|
byte[] |
encode(byte[] src)
Encodes all bytes from the specified byte array into a newly-allocated
byte array using the
Base64 encoding scheme. |
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. |
ByteBuffer |
encode(ByteBuffer buffer)
Encodes all remaining bytes from the specified byte buffer into
a newly-allocated ByteBuffer using the
Base64 encoding
scheme. |
int |
encode(ByteBuffer src,
ByteBuffer dst,
int bytesOut)
Encodes as many bytes as possible from the input byte buffer
using the
Base64 encoding scheme, writing the resulting
bytes to the given output byte buffer. |
String |
encodeToString(byte[] src)
Encodes the specified byte array into a String using the
Base64
encoding scheme. |
OutputStream |
wrap(OutputStream os)
Wraps an output stream for encoding byte data using the
Base64
encoding scheme. |
public byte[] encode(byte[] src)
Base64
encoding scheme. The returned byte
array is of the length of the resulting bytes.src
- the byte array to encodepublic int encode(byte[] src, byte[] dst)
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.
src
- the byte array to encodedst
- the output byte arrayIllegalArgumentException
- if dst
does not have enough
space for encoding all input bytes.public String encodeToString(byte[] src)
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)
.
src
- the byte array to encodepublic ByteBuffer encode(ByteBuffer buffer)
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.buffer
- the source ByteBuffer to encodepublic int encode(ByteBuffer src, ByteBuffer dst, int bytesOut)
Base64
encoding scheme, writing the resulting
bytes to the given output byte buffer.
The buffers are read from, and written to, starting at their current positions. Upon return, the input and output buffers' positions will be advanced to reflect the bytes read and written, but their limits will not be modified.
The encoding operation will stop and return if either all
remaining bytes in the input buffer have been encoded and written
to the output buffer, or the output buffer has insufficient space
to encode any more input bytes. The encoding operation can be
continued, if there is more bytes in input buffer to be encoded,
by invoking this method again with an output buffer that has more
remaining bytes. This is
typically done by draining any encoded bytes from the output buffer.
The value returned from last invocation needs to be passed in as the
third parameter bytesOut
if it is to continue an unfinished
encoding, 0 otherwise.
Recommended Usage Example
ByteBuffer src = ...; ByteBuffer dst = ...; Base64.Encoder enc = Base64.getMimeDecoder(); int bytesOut = 0; while (src.hasRemaining()) { // clear output buffer for decoding dst.clear(); bytesOut = enc.encode(src, dst, bytesOut); // read encoded bytes out of "dst" dst.flip(); ... }
src
- the input byte buffer to encodedst
- the output byte bufferbytesOut
- the return value of last invocation if this is to continue
an unfinished encoding operation, 0 otherwisebytesOut
and the number of bytes
written to the output ByteBuffer during this invocation.public OutputStream wrap(OutputStream os)
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.
os
- the output stream.
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2013, Oracle and/or its affiliates. All rights reserved.
DRAFT ea-b92