public class ByteArrayBuffer extends OutputStream
It works in a way similar to ByteArrayOutputStream
but
this class works better in the following ways:
newInputStream()
that creates a new InputStream
that won't cause buffer reallocation.
write(InputStream)
method that reads the entirety of the
given InputStream
without using a temporary buffer.
Modifier and Type | Field and Description |
---|---|
protected byte[] |
buf
The buffer where data is stored.
|
Constructor and Description |
---|
ByteArrayBuffer()
Creates a new byte array output stream.
|
ByteArrayBuffer(byte[] data) |
ByteArrayBuffer(byte[] data,
int length) |
ByteArrayBuffer(int size)
Creates a new byte array output stream, with a buffer capacity of
the specified size, in bytes.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
byte[] |
getRawData()
Gets the underlying buffer that this
ByteArrayBuffer uses. |
InputStream |
newInputStream()
Creates a new
InputStream that reads from this buffer. |
InputStream |
newInputStream(int start,
int length)
Creates a new
InputStream that reads a part of this bfufer. |
void |
reset() |
int |
size() |
byte[] |
toByteArray()
Deprecated.
this method causes a buffer reallocation. Use it only when
you have to.
|
String |
toString()
Decodes the contents of this buffer by the default encoding
and returns it as a string.
|
void |
write(byte[] b,
int off,
int len) |
void |
write(InputStream in)
Reads all the data of the given
InputStream and appends them
into this buffer. |
void |
write(int b) |
void |
writeTo(OutputStream out) |
flush, write
public ByteArrayBuffer()
public ByteArrayBuffer(int size)
size
- the initial size.IllegalArgumentException
- if size is negative.public ByteArrayBuffer(byte[] data)
public ByteArrayBuffer(byte[] data, int length)
public final void write(InputStream in) throws IOException
InputStream
and appends them
into this buffer.IOException
- if the read operation fails with an IOException
.public final void write(int b)
write
in class OutputStream
public final void write(byte[] b, int off, int len)
write
in class OutputStream
public final void writeTo(OutputStream out) throws IOException
IOException
public final void reset()
public final byte[] toByteArray()
Since this method needs to allocate a new byte[], this method will be costly.
public final int size()
public final byte[] getRawData()
ByteArrayBuffer
uses.
It's never small than its size()
.
Use with caution.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
IOException
public final InputStream newInputStream()
InputStream
that reads from this buffer.public final InputStream newInputStream(int start, int length)
InputStream
that reads a part of this bfufer.Copyright © 2015 Oracle Corporation. All rights reserved.