Top Description Fields Constructors Methods
org.apache.tomcat.util.http.fileupload

public Class ByteArrayOutputStream

extends OutputStream
Class Inheritance
Imports
java.io.IOException, .OutputStream, java.util.ArrayList, .List

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.

This is an alternative implementation of the java.io.ByteArrayOutputStream class. The original implementation only allocates 32 bytes at the beginning. As this class is designed for heavy duty it starts at 1024 bytes. In contrast to the original it doesn't reallocate the whole memory block but allocates additional buffers. This way no buffers need to be garbage collected and the contents don't have to be copied to the new buffer. This class is designed to behave exactly like the original. The only exception is the deprecated toString(int) method that has been ignored.

Field Summary

Modifier and TypeField and Description
private final List<byte[]>
buffers

The list of buffers, which grows and never reduces.

private int
count

The total count of bytes written.

private byte[]
currentBuffer

The current buffer.

private int
currentBufferIndex

The index of the current buffer.

pack-priv static final int
private static final byte[]
EMPTY_BYTE_ARRAY

A singleton empty byte array.

private int
filledBufferSum

The total count of bytes in all the filled buffers.

Constructor Summary

AccessConstructor and Description
public
ByteArrayOutputStream()

Creates a new byte array output stream.

public
ByteArrayOutputStream(final int
the initial size
size
)

Creates a new byte array output stream, 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
needNewBuffer(final int
the size of the buffer if one is created
newcount
)

Makes a new buffer available either by allocating a new one or re-cycling an existing one.

public synchronized byte[]

Returns:

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

Gets the current contents of this byte stream as a byte array.

public void
write(final byte[]
the bytes to write
b
,
final int
The start offset
off
,
final int
The number of bytes to write
len
)

Overrides java.io.OutputStream.write.

Write the bytes to byte array.

public synchronized void
write(final int
the byte to write
b
)

Implements abstract java.io.OutputStream.write.

Write a byte to byte array.

public synchronized void
writeTo(final OutputStream
the output stream to write to
out
)

Writes the entire contents of this byte stream to the specified output stream.

Inherited from java.io.OutputStream:
flushnullOutputStreamwrite

Field Detail

buffersback to summary
private final List<byte[]> buffers

The list of buffers, which grows and never reduces.

countback to summary
private int count

The total count of bytes written.

currentBufferback to summary
private byte[] currentBuffer

The current buffer.

currentBufferIndexback to summary
private int currentBufferIndex

The index of the current buffer.

DEFAULT_SIZEback to summary
pack-priv static final int DEFAULT_SIZE
EMPTY_BYTE_ARRAYback to summary
private static final byte[] EMPTY_BYTE_ARRAY

A singleton empty byte array.

filledBufferSumback to summary
private int filledBufferSum

The total count of bytes in all the filled buffers.

Constructor Detail

ByteArrayOutputStreamback to summary
public ByteArrayOutputStream()

Creates a new byte array output stream. The buffer capacity is initially 1024 bytes, though its size increases if necessary.

ByteArrayOutputStreamback to summary
public ByteArrayOutputStream(final int size)

Creates a new byte array output stream, 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:
never (this method should not declare this exception but it has to now due to backwards compatibility)
needNewBufferback to summary
private void needNewBuffer(final int newcount)

Makes a new buffer available either by allocating a new one or re-cycling an existing one.

Parameters
newcount:int

the size of the buffer if one is created

toByteArrayback to summary
public synchronized byte[] toByteArray()

Gets the current contents of this byte stream as a byte array. The result is independent of this stream.

Returns:byte[]

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

See Also
java.io.ByteArrayOutputStream#toByteArray()
writeback to summary
public void write(final byte[] b, final int off, final int len)

Overrides java.io.OutputStream.write.

Write the bytes to byte array.

Parameters
b:byte[]

the bytes to write

off:int

The start offset

len:int

The number of bytes to write

Annotations
@Override
writeback to summary
public synchronized void write(final int b)

Implements abstract java.io.OutputStream.write.

Write a byte to byte array.

Parameters
b:int

the byte to write

Annotations
@Override
writeToback to summary
public synchronized void writeTo(final OutputStream out) throws IOException

Writes the entire contents of this byte stream to the specified output stream.

Parameters
out:OutputStream

the output stream to write to

Exceptions
IOException:
if an I/O error occurs, such as if the stream is closed
See Also
java.io.ByteArrayOutputStream#writeTo(OutputStream)