public abstract class AbstractByteArrayOutputStream extends OutputStream
The data can be retrieved using toByteArray()
and
toString()
.
Closing an AbstractByteArrayOutputStream
has no effect. The methods in
this class can be called after the stream has been closed without
generating an IOException
.
This is the base for an alternative implementation of the
ByteArrayOutputStream
class. The original implementation
only allocates 32 bytes at the beginning. As this class is designed for
heavy duty it starts at 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
ByteArrayOutputStream.toString(int)
method that has been
ignored.
Modifier and Type | Class and Description |
---|---|
protected static interface |
AbstractByteArrayOutputStream.InputStreamConstructor<T extends InputStream>
Constructor for an InputStream subclass.
|
Modifier and Type | Field and Description |
---|---|
protected int |
count
The total count of bytes written.
|
Constructor and Description |
---|
AbstractByteArrayOutputStream() |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closing a
ByteArrayOutputStream has no effect. |
protected void |
needNewBuffer(int newcount)
Makes a new buffer available either by allocating
a new one or re-cycling an existing one.
|
abstract void |
reset() |
protected void |
resetImpl() |
abstract int |
size()
Returns the current size of the byte array.
|
abstract byte[] |
toByteArray()
Gets the current contents of this byte stream as a byte array.
|
protected byte[] |
toByteArrayImpl()
Gets the current contents of this byte stream as a byte array.
|
abstract InputStream |
toInputStream()
Gets the current contents of this byte stream as a Input Stream.
|
protected <T extends InputStream> |
toInputStream(AbstractByteArrayOutputStream.InputStreamConstructor<T> isConstructor)
Gets the current contents of this byte stream as a Input Stream.
|
String |
toString()
Deprecated.
2.5 use
toString(String) instead |
String |
toString(Charset charset)
Gets the current contents of this byte stream as a string
using the specified encoding.
|
String |
toString(String enc)
Gets the current contents of this byte stream as a string
using the specified encoding.
|
abstract void |
write(byte[] b,
int off,
int len)
Writes the bytes to the byte array.
|
abstract int |
write(InputStream in)
Writes the entire contents of the specified input stream to this
byte stream.
|
abstract void |
write(int b)
Write a byte to byte array.
|
protected void |
writeImpl(byte[] b,
int off,
int len)
Writes the bytes to the byte array.
|
protected int |
writeImpl(InputStream in)
Writes the entire contents of the specified input stream to this
byte stream.
|
protected void |
writeImpl(int b)
Write a byte to byte array.
|
abstract void |
writeTo(OutputStream out)
Writes the entire contents of this byte stream to the
specified output stream.
|
protected void |
writeToImpl(OutputStream out)
Writes the entire contents of this byte stream to the
specified output stream.
|
flush, write
protected int count
public AbstractByteArrayOutputStream()
protected void needNewBuffer(int newcount)
newcount
- the size of the buffer if one is createdpublic abstract 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 writeprotected void writeImpl(byte[] b, int off, int len)
b
- the bytes to writeoff
- The start offsetlen
- The number of bytes to writepublic abstract void write(int b)
write
in class OutputStream
b
- the byte to writeprotected void writeImpl(int b)
b
- the byte to writepublic abstract int write(InputStream in) throws IOException
in
- the input stream to read fromIOException
- if an I/O error occurs while reading the input streamprotected int writeImpl(InputStream in) throws IOException
in
- the input stream to read fromIOException
- if an I/O error occurs while reading the input streampublic abstract int size()
public void close() throws IOException
ByteArrayOutputStream
has no effect. The methods in
this class can be called after the stream has been closed without
generating an 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 compatibility)public abstract void reset()
ByteArrayOutputStream.reset()
protected void resetImpl()
ByteArrayOutputStream.reset()
public abstract 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)
protected void writeToImpl(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 abstract InputStream toInputStream()
this
stream,
avoiding memory allocation and copy, thus saving space and time.ByteArrayOutputStream.toByteArray()
,
reset()
protected <T extends InputStream> InputStream toInputStream(AbstractByteArrayOutputStream.InputStreamConstructor<T> isConstructor)
this
stream,
avoiding memory allocation and copy, thus saving space and time.T
- the type of the InputStream which makes up
the SequenceInputStream
.isConstructor
- A constructor for an InputStream which makes
up the SequenceInputStream
.ByteArrayOutputStream.toByteArray()
,
reset()
public abstract byte[] toByteArray()
ByteArrayOutputStream.toByteArray()
protected byte[] toByteArrayImpl()
ByteArrayOutputStream.toByteArray()
@Deprecated public String toString()
toString(String)
insteadtoString
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)
public String toString(Charset charset)
charset
- the character encodingByteArrayOutputStream.toString(String)
Copyright © 2002–2020 The Apache Software Foundation. All rights reserved.