public class ByteArrayOutputStream extends OutputStream
This class is an alternate version of
Java ByteArrayOutputStream
. This code is derived from the
Apache ByteArrayOutputStream
.
Like the Apache version, this class creates new byte arrays as it grows
without copying them into a larger one (for better performance).
Each new array is stored in a list.
In addition to the Apache version, this class offers methods to access subsets of bytes ranging form zero to the total number of bytes written so far. Also different, each byte array created have the same length, matching the initial capacity provided (default is 1024).
The higher the initial capacity, the faster it should be to write large streams, but the more initial memory it will take.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_INITIAL_CAPACITY |
Constructor and Description |
---|
ByteArrayOutputStream()
Creates a new byte array output stream.
|
ByteArrayOutputStream(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()
Closing a ByteArrayOutputStream has no effect.
|
int |
getByte(int offset)
Gets the single byte value found at specified offset.
|
int |
getBytes(byte[] target,
int offset)
Gets a byte array matching the specified offset and target
byte array length.
|
void |
reset() |
int |
size()
Return the current size of the byte array.
|
static InputStream |
toBufferedInputStream(InputStream input)
Fetches entire contents of an
InputStream and represent
same data as result InputStream. |
byte[] |
toByteArray()
Gets the current contents of this byte stream as a byte array.
|
String |
toString()
Gets the current contents of this byte stream as a string.
|
String |
toString(String enc)
Gets the current contents of this byte stream as a string
using the specified encoding.
|
void |
write(byte[] b,
int off,
int len)
Write the bytes to byte array.
|
int |
write(InputStream in)
Writes the entire contents of the specified input stream to this
byte stream.
|
void |
write(int b)
Write a byte to byte array.
|
void |
writeTo(OutputStream out)
Writes the entire contents of this byte stream to the
specified output stream.
|
flush, write
public static final int DEFAULT_INITIAL_CAPACITY
public ByteArrayOutputStream()
public ByteArrayOutputStream(int size)
size
- the initial sizeIllegalArgumentException
- if size is negativepublic int getByte(int offset)
-1
is
returned. If the offset is lower than zero, zero is assumed.offset
- position to read the byte at.public int getBytes(byte[] target, int offset)
-1
will be returned.
If the offset is lower than zero, zero is assumed.
If the target byte array length is larger than the byte array,
the value returned will be lower than the specified length.target
- target byte array to store bytes into.offset
- position to read the byte at.public void write(byte[] b, int off, int len)
write
in class OutputStream
b
- the bytes to writeoff
- The start offsetlen
- The number of bytes to writepublic void write(int b)
write
in class OutputStream
b
- the byte to writepublic int write(InputStream in) throws IOException
in
- the input stream to read fromIOException
- if an I/O error occurs while reading the input streampublic int size()
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
IOException
- never (this method should not declare this exception
but it has to now due to backwards compatability)public void reset()
ByteArrayOutputStream.reset()
public void writeTo(OutputStream out) throws IOException
out
- the output stream to write toIOException
- if an I/O error occurs, such as if the stream is
closedByteArrayOutputStream.writeTo(OutputStream)
public static InputStream toBufferedInputStream(InputStream input) throws IOException
InputStream
and represent
same data as result InputStream.
This method is useful where,
toByteArray()
, since it
avoids unnecessary allocation and copy of byte[].BufferedInputStream
.input
- Stream to be fully buffered.IOException
- if an I/O error occurspublic byte[] toByteArray()
ByteArrayOutputStream.toByteArray()
public String toString()
toString
in class Object
ByteArrayOutputStream.toString()
public String toString(String enc) throws UnsupportedEncodingException
enc
- the name of the character encodingUnsupportedEncodingException
- if the encoding is not supportedByteArrayOutputStream.toString(String)
Copyright © 2008–2021 Norconex Inc.. All rights reserved.