public final class ByteArrayOutputStream
extends java.io.OutputStream
OutputStream that buffers all written content in byte arrays. In contrast to ByteArrayOutputStream, it allows to set a maximum
buffer size, switching to a count-only mode when the maximum buffer size will be exceeded.| Modifier and Type | Field and Description |
|---|---|
static int |
MIN_CHUNK_SIZE
The default minimum chunk size.
|
| Constructor and Description |
|---|
ByteArrayOutputStream()
Creates an unlimited
ByteArrayOutputStream using #MIN_CHUNK_SIZE as minimal chunk size. |
ByteArrayOutputStream(int minChunkSize)
Creates an unlimited
ByteArrayOutputStream using the given minimal chunk size. |
ByteArrayOutputStream(int minChunkSize,
int bufferSizeLimit)
Creates a
ByteArrayOutputStream that's limited in size using the given minimal chunk size. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
bufferOverflow()
Returns whether there was a buffer overflow, i.e. the content written was larger than the maximum buffer size.
|
int |
length()
Get the overall length of the written content.
|
void |
reset()
Clear all buffers.
|
void |
trim()
Reduce the memory allocated by this object by freeing all unused chunks.
|
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
void |
writeTo(java.io.OutputStream out)
Write the buffered content to the given
OutputStream. |
public static final int MIN_CHUNK_SIZE
public ByteArrayOutputStream()
ByteArrayOutputStream using #MIN_CHUNK_SIZE as minimal chunk size.public ByteArrayOutputStream(int minChunkSize)
ByteArrayOutputStream using the given minimal chunk size.minChunkSize - The minimal chunk size.public ByteArrayOutputStream(int minChunkSize,
int bufferSizeLimit)
ByteArrayOutputStream that's limited in size using the given minimal chunk size. If the buffer size is exceeded this instance will stop
buffering and only cound the number of bytes written.
Note that the actually used memory might be larger by the chunk size than the given buffer size.
minChunkSize - The minimal chunk size.bufferSizeLimit - The maximum buffer size used by this ByteArrayOutputStream.public void write(byte[] b,
int off,
int len)
throws java.io.IOException
write in class java.io.OutputStreamjava.io.IOExceptionpublic void write(int b)
throws java.io.IOException
write in class java.io.OutputStreamjava.io.IOExceptionpublic void writeTo(java.io.OutputStream out)
throws java.io.IOException
OutputStream. Call bufferOverflow() before calling this to check if all written content was
buffered or not.out - The OutputStream to write to.java.io.IOException{@link - IllegalStateException} if there was a buffer overflow and not all content was buffered.public void reset()
public void trim()
public int length()
ByteArrayOutputStream.public boolean bufferOverflow()
true if there was a buffer overflow and not all content was buffered, false otherwise.