Package com.helger.commons.io.stream
Class NonBlockingByteArrayOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- com.helger.commons.io.stream.NonBlockingByteArrayOutputStream
-
- All Implemented Interfaces:
IWriteToStream,IHasSize,Closeable,Flushable,AutoCloseable
public class NonBlockingByteArrayOutputStream extends OutputStream implements IHasSize, IWriteToStream
A non-synchronized copy of the classByteArrayOutputStream.- Author:
- Philip Helger
- See Also:
ByteArrayOutputStream
-
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closing aByteArrayOutputStreamhas no effect.byte[]directGetBuffer()NonBlockingByteArrayInputStreamgetAsInputStream()Create a new InputStream from the contained byte array WITHOUT COPYING it.StringgetAsString(int nOfs, int nLength, Charset aCharset)Converts the buffer's contents into a string by decoding the bytes using the specifiedcharsetName.StringgetAsString(int nLength, Charset aCharset)Converts the buffer's contents into a string by decoding the bytes using the specifiedcharsetName.StringgetAsString(Charset aCharset)Converts the buffer's contents into a string by decoding the bytes using the specifiedcharsetName.byte[]getBufferOrCopy()intgetBufferSize()bytegetByteAt(int nIndex)Get the byte at the specified indexbooleanisEmpty()booleanisNotEmpty()voidreadFrom(InputStream aIS)Reads the givenInputStreamcompletely into the buffer.voidreset()Resets thecountfield of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded.intsize()Returns the current size of the buffer.booleanstartsWith(byte[] aBytes)booleanstartsWith(byte[] aBytes, int nOfs, int nLen)byte[]toByteArray()Creates a newly allocated byte array.StringtoString()voidwrite(byte[] aBuf)voidwrite(byte[] aBuf, int nOfs, int nLen)WritesnLenbytes from the specified byte array starting at offsetnOfsto this byte array output stream.voidwrite(int b)Writes the specified byte to this byte array output stream.voidwriteTo(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 usingout.write(buf, 0, count).-
Methods inherited from class java.io.OutputStream
flush, nullOutputStream
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.helger.commons.io.IWriteToStream
writeToAndClose
-
-
-
-
Constructor Detail
-
NonBlockingByteArrayOutputStream
public NonBlockingByteArrayOutputStream()
Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary.
-
NonBlockingByteArrayOutputStream
public NonBlockingByteArrayOutputStream(@Nonnegative int nSize)
Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.- Parameters:
nSize- the initial size.- Throws:
IllegalArgumentException- if size is negative.
-
-
Method Detail
-
write
public void write(int b)
Writes the specified byte to this byte array output stream.- Specified by:
writein classOutputStream- Parameters:
b- the byte to be written.
-
write
public void write(@Nonnull byte[] aBuf)
- Overrides:
writein classOutputStream
-
write
public void write(@Nonnull byte[] aBuf, int nOfs, int nLen)
WritesnLenbytes from the specified byte array starting at offsetnOfsto this byte array output stream.- Overrides:
writein classOutputStream- Parameters:
aBuf- the data.nOfs- the start offset in the data.nLen- the number of bytes to write.
-
writeTo
public void writeTo(@Nonnull @WillNotClose OutputStream aOS) throws IOException
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 usingout.write(buf, 0, count). The content of this stream is not altered by calling this method.- Specified by:
writeToin interfaceIWriteToStream- Parameters:
aOS- the output stream to which to write the data. May not benull.- Throws:
IOException- if an I/O error occurs.
-
readFrom
public void readFrom(@Nonnull @WillNotClose InputStream aIS) throws IOException
Reads the givenInputStreamcompletely into the buffer.- Parameters:
aIS- the InputStream to read from. May not benull. Is not closed internally.- Throws:
IOException- If reading fails
-
reset
public void reset()
Resets thecountfield 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.
-
toByteArray
@Nonnull @ReturnsMutableCopy public byte[] toByteArray()
Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.
If you are sure, that this OutputStream is not altered anymore, it maybe preferred to usegetBufferOrCopy()because it avoids copying the internal buffer if the size matches exactly.- Returns:
- the current contents of this output stream, as a byte array.
-
getByteAt
public byte getByteAt(@Nonnegative int nIndex)
Get the byte at the specified index- Parameters:
nIndex- The index to use. Must be ≥ 0 and < count- Returns:
- The byte at the specified position
-
getBufferSize
@Nonnegative public int getBufferSize()
- Returns:
- The number of pre-allocated bytes. Always ≥ 0.
-
size
@Nonnegative public int size()
Returns the current size of the buffer.
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceIHasSize- Returns:
trueif no items are present,falseif at least a single item is present.- See Also:
IHasSize.size(),IHasSize.isNotEmpty()
-
isNotEmpty
public boolean isNotEmpty()
- Specified by:
isNotEmptyin interfaceIHasSize- Returns:
trueif at least one item is present,falseif no item is present.- See Also:
IHasSize.size(),IHasSize.isEmpty()
-
startsWith
public boolean startsWith(@Nonnull byte[] aBytes)
-
startsWith
public boolean startsWith(@Nonnull byte[] aBytes, @Nonnegative int nOfs, @Nonnegative int nLen)
-
getAsString
@Nonnull public String getAsString(@Nonnull Charset aCharset)
Converts the buffer's contents into a string by decoding the bytes using the specifiedcharsetName. The length of the newStringis 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
CharsetDecoderclass should be used when more control over the decoding process is required.- Parameters:
aCharset- the charset to be used. May not benull.- Returns:
- String decoded from the buffer's contents.
-
getAsString
@Nonnull public String getAsString(@Nonnegative int nLength, @Nonnull Charset aCharset)
Converts the buffer's contents into a string by decoding the bytes using the specifiedcharsetName. The length of the newStringis 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
CharsetDecoderclass should be used when more control over the decoding process is required.- Parameters:
nLength- The number of bytes to be converted to a String. Must be ≥ 0.aCharset- the charset to be used. May not benull.- Returns:
- String decoded from the buffer's contents.
-
getAsString
@Nonnull public String getAsString(@Nonnegative int nOfs, @Nonnegative int nLength, @Nonnull Charset aCharset)
Converts the buffer's contents into a string by decoding the bytes using the specifiedcharsetName. The length of the newStringis 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
CharsetDecoderclass should be used when more control over the decoding process is required.- Parameters:
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 benull.- Returns:
- String decoded from the buffer's contents.
-
directGetBuffer
@Nonnull @ReturnsMutableObject public byte[] directGetBuffer()
- Returns:
- The internally used byte buffer. Never
null. Handle with care!
-
getBufferOrCopy
@Nonnull public byte[] getBufferOrCopy()
- Returns:
- The direct buffer, if the array size and the count are identical, a copy otherwise.
- Since:
- 9.1.3
- See Also:
size(),directGetBuffer(),toByteArray()
-
close
public void close()
Closing aByteArrayOutputStreamhas no effect. The methods in this class can be called after the stream has been closed without generating anIOException. This operation does nothing on this class.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream
-
getAsInputStream
@Nonnull public NonBlockingByteArrayInputStream getAsInputStream()
Create a new InputStream from the contained byte array WITHOUT COPYING it. So please be careful as this method is not thread-safe and any modifications done later on this object are NOT reflected in the InputStream!
This is a shortcut fornew NonBlockingByteArrayInputStream (directGetBuffer (), 0, getSize ())- Returns:
- A new
NonBlockingByteArrayInputStream. - Since:
- 9.0.0
-
-