public class NonBlockingByteArrayOutputStream extends OutputStream implements IHasSize, IWriteToStream, Serializable
ByteArrayOutputStream.ByteArrayOutputStream,
Serialized Form| Modifier and Type | Field and Description |
|---|---|
protected byte[] |
m_aBuf
The buffer where data is stored.
|
protected int |
m_nCount
The number of valid bytes in the buffer.
|
| Constructor and Description |
|---|
NonBlockingByteArrayOutputStream()
Creates a new byte array output stream.
|
NonBlockingByteArrayOutputStream(int nSize)
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.
|
byte[] |
directGetBuffer() |
String |
getAsString(Charset aCharset)
Converts the buffer's contents into a string by decoding the bytes using
the specified
charsetName. |
String |
getAsString(int nLength,
Charset aCharset)
Converts the buffer's contents into a string by decoding the bytes using
the specified
charsetName. |
String |
getAsString(int nOfs,
int nLength,
Charset aCharset)
Converts the buffer's contents into a string by decoding the bytes using
the specified
charsetName. |
int |
getBufferSize() |
byte |
getByteAt(int nIndex)
Get the byte at the specified index
|
int |
getSize()
Returns the current size of the buffer.
|
boolean |
isEmpty() |
boolean |
isNotEmpty() |
void |
readFrom(InputStream aIS)
Reads the given
InputStream completely into the buffer. |
void |
reset()
Resets the
count field of this byte array output stream to
zero, so that all currently accumulated output in the output stream is
discarded. |
boolean |
startsWith(byte[] aBytes) |
boolean |
startsWith(byte[] aBytes,
int nOfs,
int nLen) |
byte[] |
toByteArray()
Creates a newly allocated byte array.
|
String |
toString() |
void |
write(byte[] aBuf) |
void |
write(byte[] aBuf,
int nOfs,
int nLen)
Writes
nLen bytes from the specified byte array starting at
offset nOfs to this byte array output stream. |
void |
write(int b)
Writes the specified byte to this byte array output stream.
|
void |
writeTo(OutputStream aOS)
Writes the complete contents of this byte array output stream to the
specified output stream argument, as if by calling the output stream's
write method using
out.write(buf, 0, count). |
flushclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitwriteToAndCloseprotected byte[] m_aBuf
protected int m_nCount
public NonBlockingByteArrayOutputStream()
public NonBlockingByteArrayOutputStream(@Nonnegative int nSize)
nSize - the initial size.IllegalArgumentException - if size is negative.public void write(int b)
write in class OutputStreamb - the byte to be written.public void write(@Nonnull byte[] aBuf)
write in class OutputStreampublic void write(@Nonnull byte[] aBuf, int nOfs, int nLen)
nLen bytes from the specified byte array starting at
offset nOfs to this byte array output stream.write in class OutputStreamaBuf - the data.nOfs - the start offset in the data.nLen - the number of bytes to write.public void writeTo(@Nonnull @WillNotClose OutputStream aOS) throws IOException
out.write(buf, 0, count). The content of
this stream is not altered by calling this method.writeTo in interface IWriteToStreamaOS - the output stream to which to write the data. May not be
null.IOException - if an I/O error occurs.public void readFrom(@Nonnull @WillNotClose InputStream aIS) throws IOException
InputStream completely into the buffer.aIS - the InputStream to read from. May not be null. Is not
closed internally.IOException - If reading failspublic void reset()
count field of this byte array output stream to
zero, so that all currently accumulated output in the output stream is
discarded. The output stream can be used again, reusing the already
allocated buffer space.@Nonnull @ReturnsMutableCopy public byte[] toByteArray()
public byte getByteAt(@Nonnegative int nIndex)
nIndex - The index to use. Must be ≥ 0 and < count@Nonnegative public int getSize()
@Nonnegative public int getBufferSize()
public boolean isEmpty()
isEmpty in interface IHasSizetrue if no items are present, false if at
least a single item is present.IHasSize.getSize(),
IHasSize.isNotEmpty()public boolean isNotEmpty()
isNotEmpty in interface IHasSizetrue if at least one item is present,
false if no item is present.IHasSize.getSize(),
IHasSize.isEmpty()public boolean startsWith(@Nonnull byte[] aBytes)
public boolean startsWith(@Nonnull byte[] aBytes, @Nonnegative int nOfs, @Nonnegative int nLen)
@Nonnull public String getAsString(@Nonnull Charset aCharset)
charsetName. The length of
the new String is a function of the charset, and hence may not be
equal to the length of the byte array.
This method always replaces malformed-input and unmappable-character
sequences with this charset's default replacement string. The
CharsetDecoder class should be used when more
control over the decoding process is required.
aCharset - the charset to be used. May not be null.@Nonnull public String getAsString(@Nonnegative int nLength, @Nonnull Charset aCharset)
charsetName. The length of
the new String is a function of the charset, and hence may not be
equal to the length of the byte array.
This method always replaces malformed-input and unmappable-character
sequences with this charset's default replacement string. The
CharsetDecoder class should be used when more
control over the decoding process is required.
nLength - The number of bytes to be converted to a String. Must be ≥ 0.aCharset - the charset to be used. May not be null.@Nonnull public String getAsString(@Nonnegative int nOfs, @Nonnegative int nLength, @Nonnull Charset aCharset)
charsetName. The length of
the new String is a function of the charset, and hence may not be
equal to the length of the byte array.
This method always replaces malformed-input and unmappable-character
sequences with this charset's default replacement string. The
CharsetDecoder class should be used when more
control over the decoding process is required.
nOfs - The start index to usenLength - The number of bytes to be converted to a String. Must be ≥ 0.aCharset - the charset to be used. May not be null.@Nonnull @ReturnsMutableObject(value="by design") public byte[] directGetBuffer()
null. Handle
with care!public void close()
close in interface Closeableclose in interface AutoCloseableclose in class OutputStreamCopyright © 2014–2017 Philip Helger. All rights reserved.