public class NonBlockingByteArrayOutputStream extends OutputStream implements IHasSize, IWriteToStream, Serializable
ByteArrayOutputStream
.ByteArrayOutputStream
,
Serialized FormModifier 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() |
NonBlockingByteArrayInputStream |
getAsInputStream()
Create a new InputStream from the contained byte array WITHOUT
COPYING it.
|
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 . |
byte[] |
getBufferOrCopy() |
int |
getBufferSize() |
byte |
getByteAt(int nIndex)
Get the byte at the specified index
|
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. |
int |
size()
Returns the current size of the buffer.
|
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) . |
flush
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
writeToAndClose
protected 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 OutputStream
b
- the byte to be written.public void write(@Nonnull byte[] aBuf)
write
in class OutputStream
public 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 OutputStream
aBuf
- 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 IWriteToStream
aOS
- 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()
getBufferOrCopy()
because it avoids copying the
internal buffer if the size matches exactly.public byte getByteAt(@Nonnegative int nIndex)
nIndex
- The index to use. Must be ≥ 0 and < count@Nonnegative public int getBufferSize()
@Nonnegative public int size()
public boolean isEmpty()
isEmpty
in interface IHasSize
true
if no items are present, false
if at
least a single item is present.IHasSize.size()
,
IHasSize.isNotEmpty()
public boolean isNotEmpty()
isNotEmpty
in interface IHasSize
true
if at least one item is present,
false
if no item is present.IHasSize.size()
,
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 public byte[] directGetBuffer()
null
. Handle
with care!@Nonnull public byte[] getBufferOrCopy()
size()
,
directGetBuffer()
,
toByteArray()
public void close()
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
@Nonnull public NonBlockingByteArrayInputStream getAsInputStream()
new NonBlockingByteArrayInputStream (directGetBuffer (), 0, getSize ())
NonBlockingByteArrayInputStream
.Copyright © 2014–2019 Philip Helger. All rights reserved.