Top Description Fields Constructors Methods
java.io

public Class ByteArrayOutputStream

extends OutputStream
Class Inheritance
Known Direct Subclasses
com.sun.crypto.provider.AEADBufferedStream, jdk.internal.vm.ThreadDumper.BoundedByteArrayOutputStream, sun.security.util.DerOutputStream, sun.net.www.http.PosterOutputStream, sun.security.ssl.HandshakeOutStream, sun.security.ssl.OutputRecord
Imports
java.nio.charset.Charset, java.util.Arrays, .Objects, jdk.internal.util.ArraysSupport

This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using 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.

Author
Arthur van Hoff
Since
1.0

Field Summary

Modifier and TypeField and Description
protected byte[]
buf

The buffer where data is stored.

protected int
count

The number of valid bytes in the buffer.

Constructor Summary

AccessConstructor and Description
public
ByteArrayOutputStream()

Creates a new ByteArrayOutputStream.

public
ByteArrayOutputStream(int
the initial size.
size
)

Creates a new ByteArrayOutputStream, with a buffer capacity of the specified size, in bytes.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Closing a ByteArrayOutputStream has no effect.

private void
ensureCapacity(int
the desired minimum capacity.
minCapacity
)

Increases the capacity if necessary to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

public synchronized void
reset()

Resets the count field of this ByteArrayOutputStream to zero, so that all currently accumulated output in the output stream is discarded.

public synchronized int

Returns:

the value of the count field, which is the number of valid bytes in this output stream.
size
()

Returns the current size of the buffer.

public synchronized byte[]

Returns:

the current contents of this output stream, as a byte array.
toByteArray
()

Creates a newly allocated byte array.

public synchronized String

Returns:

String decoded from the buffer's contents.
toString
()

Overrides java.lang.Object.toString.

Converts the buffer's contents into a string decoding bytes using the default charset.

public synchronized String

Returns:

String decoded from the buffer's contents.
toString
(String
the name of a supported charset
charsetName
)

Converts the buffer's contents into a string by decoding the bytes using the named charset.

public synchronized String

Returns:

String decoded from the buffer's contents.
toString
(Charset
the charset to be used to decode the bytes
charset
)

Converts the buffer's contents into a string by decoding the bytes using the specified charset.

public synchronized String

Returns:

the current contents of the output stream, as a string.
toString
(int
the high byte of each resulting Unicode character.
hibyte
)
Deprecated This method does not properly convert bytes into characters.

Creates a newly allocated string.

public synchronized void
write(int
the byte to be written.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this ByteArrayOutputStream.

public synchronized void
write(byte[]
the data.
b
,
int
the start offset in the data.
off
,
int
the number of bytes to write.
len
)

Overrides java.io.OutputStream.write.

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

public void
writeBytes(byte[]
the data.
b
)

Writes the complete contents of the specified byte array to this ByteArrayOutputStream.

public synchronized void
writeTo(OutputStream
the output stream to which to write the data.
out
)

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).

Inherited from java.io.OutputStream:
flushnullOutputStreamwrite

Field Detail

bufback to summary
protected byte[] buf

The buffer where data is stored.

countback to summary
protected int count

The number of valid bytes in the buffer.

Constructor Detail

ByteArrayOutputStreamback to summary
public ByteArrayOutputStream()

Creates a new ByteArrayOutputStream. The buffer capacity is initially 32 bytes, though its size increases if necessary.

ByteArrayOutputStreamback to summary
public ByteArrayOutputStream(int size)

Creates a new ByteArrayOutputStream, with a buffer capacity of the specified size, in bytes.

Parameters
size:int

the initial size.

Exceptions
IllegalArgumentException:
if size is negative.

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.OutputStream.close.

if an I/O error occurs.

ensureCapacityback to summary
private void ensureCapacity(int minCapacity)

Increases the capacity if necessary to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters
minCapacity:int

the desired minimum capacity.

Exceptions
OutOfMemoryError:
if minCapacity < 0 and minCapacity - buf.length > 0. This is interpreted as a request for the unsatisfiably large capacity. (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE).
resetback to summary
public synchronized void reset()

Resets the 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.

See Also
java.io.ByteArrayInputStream#count
sizeback to summary
public synchronized int size()

Returns the current size of the buffer.

Returns:int

the value of the count field, which is the number of valid bytes in this output stream.

See Also
java.io.ByteArrayOutputStream#count
toByteArrayback to summary
public synchronized byte[] toByteArray()

Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.

Returns:byte[]

the current contents of this output stream, as a byte array.

See Also
java.io.ByteArrayOutputStream#size()
toStringback to summary
public synchronized String toString()

Overrides java.lang.Object.toString.

Converts the buffer's contents into a string decoding bytes using the default charset. The length of the new 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 java.nio.charset.CharsetDecoder class should be used when more control over the decoding process is required.

Returns:String

String decoded from the buffer's contents.

Annotations
@Override
Since
1.1
See Also
Charset#defaultCharset()
toStringback to summary
public synchronized String toString(String charsetName) throws UnsupportedEncodingException

Converts the buffer's contents into a string by decoding the bytes using the named 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("UTF-8")
behaves in exactly the same way as the expression
ByteArrayOutputStream b; b.toString(StandardCharsets.UTF_8)
ByteArrayOutputStream b;
b.toString(StandardCharsets.UTF_8)
Parameters
charsetName:String

the name of a supported charset

Returns:String

String decoded from the buffer's contents.

Exceptions
UnsupportedEncodingException:
If the named charset is not supported
Since
1.1
toStringback to summary
public synchronized String toString(Charset charset)

Converts the buffer's contents into a string by decoding the bytes using the specified 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 java.nio.charset.CharsetDecoder class should be used when more control over the decoding process is required.

Parameters
charset:Charset

the charset to be used to decode the bytes

Returns:String

String decoded from the buffer's contents.

Since
10
toStringback to summary
public synchronized String toString(int hibyte)

Deprecated

This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the 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.

Creates a newly allocated string. Its size is the current size of the output stream and the valid contents of the buffer have been copied into it. Each character c in the resulting string is constructed from the corresponding element b in the byte array such that:

c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
Parameters
hibyte:int

the high byte of each resulting Unicode character.

Returns:String

the current contents of the output stream, as a string.

Annotations
@Deprecated
See Also
java.io.ByteArrayOutputStream#size(), java.io.ByteArrayOutputStream#toString(String), java.io.ByteArrayOutputStream#toString(), Charset#defaultCharset()
writeback to summary
public synchronized void write(int b)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this ByteArrayOutputStream.

Parameters
b:int

the byte to be written.

Annotations
@Override
writeback to summary
public synchronized void write(byte[] b, int off, int len)

Overrides java.io.OutputStream.write.

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

Parameters
b:byte[]

Doc from java.io.OutputStream.write.

the data.

off:int

Doc from java.io.OutputStream.write.

the start offset in the data.

len:int

Doc from java.io.OutputStream.write.

the number of bytes to write.

Annotations
@Override
Exceptions
NullPointerException:
if b is null.
IndexOutOfBoundsException:
if off is negative, len is negative, or len is greater than b.length - off
writeBytesback to summary
public void writeBytes(byte[] b)

Writes the complete contents of the specified byte array to this ByteArrayOutputStream.

API Note

This method is equivalent to write(b, 0, b.length).

Parameters
b:byte[]

the data.

Exceptions
NullPointerException:
if b is null.
Since
11
writeToback to summary
public synchronized void writeTo(OutputStream out) throws IOException

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).

Parameters
out:OutputStream

the output stream to which to write the data.

Exceptions
IOException:
if an I/O error occurs.
NullPointerException:
if out is null.