Skip to content

Most visited

Recently visited

navigation
Added in API level 1

ByteArrayOutputStream

public class ByteArrayOutputStream
extends OutputStream

java.lang.Object
   ↳ java.io.OutputStream
     ↳ java.io.ByteArrayOutputStream


A specialized OutputStream for class for writing content to an (internal) byte array. As bytes are written to this stream, the byte array may be expanded to hold more bytes. When the writing is considered to be finished, a copy of the byte array can be requested from the class.

See also:

Summary

Fields

protected byte[] buf

The byte array containing the bytes written.

protected int count

The number of bytes written.

Public constructors

ByteArrayOutputStream()

Constructs a new ByteArrayOutputStream with a default size of 32 bytes.

ByteArrayOutputStream(int size)

Constructs a new ByteArrayOutputStream with a default size of size bytes.

Public methods

void close()

Closes this stream.

void reset()

Resets this stream to the beginning of the underlying byte array.

int size()

Returns the total number of bytes written to this stream so far.

byte[] toByteArray()

Returns the contents of this ByteArrayOutputStream as a byte array.

String toString(int hibyte)

This method was deprecated in API level 1. Use toString() instead.

String toString(String charsetName)

Returns the contents of this ByteArrayOutputStream as a string converted according to the encoding declared in charsetName.

String toString()

Returns the contents of this ByteArrayOutputStream as a string.

void write(byte[] buffer, int offset, int len)

Writes count bytes from the byte array buffer starting at offset index to this stream.

void write(int oneByte)

Writes the specified byte oneByte to the OutputStream.

void writeTo(OutputStream out)

Takes the contents of this stream and writes it to the output stream out.

Inherited methods

From class java.io.OutputStream
From class java.lang.Object
From interface java.io.Closeable
From interface java.io.Flushable
From interface java.lang.AutoCloseable

Fields

buf

Added in API level 1
byte[] buf

The byte array containing the bytes written.

count

Added in API level 1
int count

The number of bytes written.

Public constructors

ByteArrayOutputStream

Added in API level 1
ByteArrayOutputStream ()

Constructs a new ByteArrayOutputStream with a default size of 32 bytes. If more than 32 bytes are written to this instance, the underlying byte array will expand.

ByteArrayOutputStream

Added in API level 1
ByteArrayOutputStream (int size)

Constructs a new ByteArrayOutputStream with a default size of size bytes. If more than size bytes are written to this instance, the underlying byte array will expand.

Parameters
size int: initial size for the underlying byte array, must be non-negative.
Throws
IllegalArgumentException if size < 0.

Public methods

close

Added in API level 1
void close ()

Closes this stream. This releases system resources used for this stream.

Throws
IOException if an error occurs while attempting to close this stream.

reset

Added in API level 1
void reset ()

Resets this stream to the beginning of the underlying byte array. All subsequent writes will overwrite any bytes previously stored in this stream.

size

Added in API level 1
int size ()

Returns the total number of bytes written to this stream so far.

Returns
int the number of bytes written to this stream.

toByteArray

Added in API level 1
byte[] toByteArray ()

Returns the contents of this ByteArrayOutputStream as a byte array. Any changes made to the receiver after returning will not be reflected in the byte array returned to the caller.

Returns
byte[] this stream's current contents as a byte array.

toString

Added in API level 1
String toString (int hibyte)

This method was deprecated in API level 1.
Use toString() instead.

Returns the contents of this ByteArrayOutputStream as a string. Each byte b in this stream is converted to a character c using the following function: c == (char)(((hibyte & 0xff) << 8) | (b & 0xff)). This method is deprecated and either toString() or toString(String) should be used.

Parameters
hibyte int: the high byte of each resulting Unicode character.
Returns
String this stream's current contents as a string with the high byte set to hibyte.

toString

Added in API level 1
String toString (String charsetName)

Returns the contents of this ByteArrayOutputStream as a string converted according to the encoding declared in charsetName.

Parameters
charsetName String: a string representing the encoding to use when translating this stream to a string.
Returns
String this stream's current contents as an encoded string.
Throws
UnsupportedEncodingException if the provided encoding is not supported.

toString

Added in API level 1
String toString ()

Returns the contents of this ByteArrayOutputStream as a string. Any changes made to the receiver after returning will not be reflected in the string returned to the caller.

Returns
String this stream's current contents as a string.

write

Added in API level 1
void write (byte[] buffer, 
                int offset, 
                int len)

Writes count bytes from the byte array buffer starting at offset index to this stream.

Parameters
buffer byte: the buffer to be written.
offset int: the initial position in buffer to retrieve bytes.
len int: the number of bytes of buffer to write.
Throws
NullPointerException if buffer is null.
IndexOutOfBoundsException if offset < 0 or len < 0, or if offset + len is greater than the length of buffer.

write

Added in API level 1
void write (int oneByte)

Writes the specified byte oneByte to the OutputStream. Only the low order byte of oneByte is written.

Parameters
oneByte int: the byte to be written.

writeTo

Added in API level 1
void writeTo (OutputStream out)

Takes the contents of this stream and writes it to the output stream out.

Parameters
out OutputStream: an OutputStream on which to write the contents of this stream.
Throws
IOException if an error occurs while writing to out.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.