Closeable
, Flushable
, AutoCloseable
public class ByteArrayOutputStream extends OutputStream
toByteArray()
and toString()
. Closing a ByteArrayOutputStream
has no effect. The methods in this class can be called after the stream has been closed without generating an IOException
.
Modifier and Type | Field | Description |
---|---|---|
protected byte[] |
buf |
The buffer where data is stored. |
protected int |
count |
The number of valid bytes in the buffer. |
Constructor | Description |
---|---|
ByteArrayOutputStream() |
Creates a new ByteArrayOutputStream . |
ByteArrayOutputStream |
Creates a new ByteArrayOutputStream , with a buffer capacity of the specified size, in bytes. |
Modifier and Type | Method | Description |
---|---|---|
void |
close() |
Closing a ByteArrayOutputStream has no effect. |
void |
reset() |
Resets the count field of this ByteArrayOutputStream to zero, so that all currently accumulated output in the output stream is discarded. |
int |
size() |
Returns the current size of the buffer. |
byte[] |
toByteArray() |
Creates a newly allocated byte array. |
String |
toString() |
Converts the buffer's contents into a string decoding bytes using the default charset. |
String |
toString |
Deprecated. This method does not properly convert bytes into characters. |
String |
toString |
Converts the buffer's contents into a string by decoding the bytes using the named charset . |
String |
toString |
Converts the buffer's contents into a string by decoding the bytes using the specified charset . |
void |
write |
Writes len bytes from the specified byte array starting at offset off to this ByteArrayOutputStream . |
void |
write |
Writes the specified byte to this ByteArrayOutputStream . |
void |
writeBytes |
Writes the complete contents of the specified byte array to this ByteArrayOutputStream . |
void |
writeTo |
Writes the complete contents of this ByteArrayOutputStream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count) . |
flush, nullOutputStream, write
protected byte[] buf
protected int count
public ByteArrayOutputStream()
ByteArrayOutputStream
. The buffer capacity is initially 32 bytes, though its size increases if necessary.public ByteArrayOutputStream(int size)
ByteArrayOutputStream
, with a buffer capacity of the specified size, in bytes.size
- the initial size.IllegalArgumentException
- if size is negative.public void write(int b)
ByteArrayOutputStream
.write
in class OutputStream
b
- the byte to be written.public void write(byte[] b, int off, int len)
len
bytes from the specified byte array starting at offset off
to this ByteArrayOutputStream
.write
in class OutputStream
b
- the data.off
- the start offset in the data.len
- the number of bytes to write.NullPointerException
- if b
is null
.IndexOutOfBoundsException
- if off
is negative, len
is negative, or len
is greater than b.length - off
public void writeBytes(byte[] b)
ByteArrayOutputStream
.write(b, 0, b.length)
.b
- the data.NullPointerException
- if b
is null
.public void writeTo(OutputStream out) throws IOException
ByteArrayOutputStream
to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count)
.out
- the output stream to which to write the data.NullPointerException
- if out
is null
.IOException
- if an I/O error occurs.public void reset()
count
field of this ByteArrayOutputStream
to zero, so that all currently accumulated output in the output stream is discarded. The output stream can be used again, reusing the already allocated buffer space.public byte[] toByteArray()
public int size()
count
field, which is the number of valid bytes in this output stream.public String toString()
String
is a function of the charset, and hence may not be equal to the size of the buffer. This method always replaces malformed-input and unmappable-character sequences with the default replacement string for the default charset. The CharsetDecoder class should be used when more control over the decoding process is required.
public String toString(String charsetName) throws UnsupportedEncodingException
charset
. This method is equivalent to #toString(charset)
that takes a charset
.
An invocation of this method of the form
ByteArrayOutputStream b;
b.toString("UTF-8")
ByteArrayOutputStream b;
b.toString(StandardCharsets.UTF_8)
charsetName
- the name of a supported charset
UnsupportedEncodingException
- If the named charset is not supportedpublic String toString(Charset charset)
charset
. The length of the new String
is a function of the charset, and hence may not be equal to the length of the byte array. This method always replaces malformed-input and unmappable-character sequences with the charset's default replacement string. The CharsetDecoder
class should be used when more control over the decoding process is required.
charset
- the charset to be used to decode the bytes
@Deprecated public String toString(int hibyte)
toString(String charsetName)
or toString(Charset charset)
method, which takes an encoding-name or charset argument, or the toString()
method, which uses the default charset. c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
hibyte
- the high byte of each resulting Unicode character.public void close() throws IOException
ByteArrayOutputStream
has no effect. The methods in this class can be called after the stream has been closed without generating an IOException
.close
in interface AutoCloseable
close
in interface Closeable
close
in class OutputStream
IOException
- if an I/O error occurs.
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/ByteArrayOutputStream.html