Package com.helger.commons.io.stream
Class ByteBufferOutputStream
java.lang.Object
java.io.OutputStream
com.helger.commons.io.stream.ByteBufferOutputStream
- All Implemented Interfaces:
IWriteToStream,Closeable,Flushable,AutoCloseable
Wrapper for an
OutputStream around a
ByteBuffer.- Author:
- Philip Helger
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final boolean -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new object with theDEFAULT_BUF_SIZEbuffer size and it can grow.ByteBufferOutputStream(byte[] aArray) Constructor with an existing byte array to wrap.ByteBufferOutputStream(byte[] aArray, int nOfs, int nLen) Constructor with an existing byte array to wrap.ByteBufferOutputStream(int nBytes) Constructor for an output stream than can grow.ByteBufferOutputStream(int nBytes, boolean bCanGrow) ConstructorByteBufferOutputStream(ByteBuffer aBuffer, boolean bCanGrow) Constructor -
Method Summary
Modifier and TypeMethodDescriptionbooleancanGrow()voidclose()byte[]Get everything as a big byte array, without altering the ByteBuffer.getAsByteArrayInputStream(boolean bCopyNeeded) getAsByteArrayWrapper(boolean bCopyNeeded) getAsString(Charset aCharset) Get the content as a string without modifying the buffer.voidreset()Reset the backing byte bufferintsize()voidwrite(byte[] aBuf, int nOfs, int nLen) voidwrite(int b) voidwrite(ByteBuffer aSrcBuffer) Write the content from the passed byte buffer to this output stream.voidwriteTo(byte[] aBuf) Writes the current content to the passed buffer.voidwriteTo(byte[] aBuf, boolean bCompactBuffer) Writes the current content to the passed buffer.voidwriteTo(byte[] aBuf, int nOfs, int nLen) Write current content to the passed byte array.voidwriteTo(byte[] aBuf, int nOfs, int nLen, boolean bCompactBuffer) Write current content to the passed byte array.voidwriteTo(OutputStream aOS) Write everything to the passed output stream and clear the contained buffer.voidwriteTo(OutputStream aOS, boolean bClearBuffer) Write everything to the passed output stream and optionally clear the contained buffer.voidwriteTo(ByteBuffer aDestBuffer) Write everything currently contained to the specified buffer.voidwriteTo(ByteBuffer aDestBuffer, boolean bCompactBuffer) Write everything currently contained to the specified buffer.Methods inherited from class java.io.OutputStream
flush, nullOutputStream, writeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.helger.commons.io.IWriteToStream
writeToAndClose
-
Field Details
-
DEFAULT_BUF_SIZE
public static final int DEFAULT_BUF_SIZE- See Also:
-
DEFAULT_CAN_GROW
public static final boolean DEFAULT_CAN_GROW- See Also:
-
-
Constructor Details
-
ByteBufferOutputStream
public ByteBufferOutputStream()Create a new object with theDEFAULT_BUF_SIZEbuffer size and it can grow. -
ByteBufferOutputStream
Constructor for an output stream than can grow.- Parameters:
nBytes- The initial number of bytes the buffer has. Must be ≥ 0.
-
ByteBufferOutputStream
Constructor- Parameters:
nBytes- The number of bytes the buffer has initially. Must be ≥ 0.bCanGrow-trueif the buffer can grow,falseotherwise.
-
ByteBufferOutputStream
Constructor with an existing byte array to wrap. This output stream cannot grow!- Parameters:
aArray- The array to be backed by aByteBuffer.
-
ByteBufferOutputStream
Constructor with an existing byte array to wrap. This output stream cannot grow!- Parameters:
aArray- The array to be backed by aByteBuffer.nOfs- Offset into the byte array. Must be ≥ 0.nLen- Number of bytes to wrap. Must be ≥ 0.
-
ByteBufferOutputStream
Constructor- Parameters:
aBuffer- The byte buffer to use. May not benull.bCanGrow-trueif the buffer can grow,falseotherwise.
-
-
Method Details
-
getBuffer
- Returns:
- The contained buffer. Never
null.
-
canGrow
public boolean canGrow()- Returns:
trueif this buffer can grow,falseif not.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream
-
reset
public void reset()Reset the backing byte buffer -
size
- Returns:
- The number of bytes currently in the buffer. Always ≥ 0.
-
getAsByteArray
Get everything as a big byte array, without altering the ByteBuffer. This works only if the contained ByteBuffer has a backing array.- Returns:
- The content of the buffer as a byte array. Never
null.
-
getAsByteArrayInputStream
-
getAsByteArrayWrapper
-
writeTo
Write everything currently contained to the specified buffer. If the passed buffer is too small, aBufferOverflowExceptionis thrown. The copied elements are removed from this streams buffer.- Parameters:
aDestBuffer- The destination buffer to write to. May not benull.
-
writeTo
Write everything currently contained to the specified buffer. If the passed buffer is too small, aBufferOverflowExceptionis thrown. The copied elements are removed from this streams buffer.- Parameters:
aDestBuffer- The destination buffer to write to. May not benull.bCompactBuffer-trueto compact the buffer afterwards,falseotherwise.
-
writeTo
Writes the current content to the passed buffer. The copied elements are removed from this streams buffer.- Parameters:
aBuf- The buffer to be filled. May not benull.
-
writeTo
Writes the current content to the passed buffer. The copied elements are removed from this streams buffer.- Parameters:
aBuf- The buffer to be filled. May not benull.bCompactBuffer-trueto compact the buffer afterwards,falseotherwise.
-
writeTo
Write current content to the passed byte array. The copied elements are removed from this streams buffer.- Parameters:
aBuf- Byte array to write to. May not benull.nOfs- Offset to start writing. Must be ≥ 0.nLen- Number of bytes to copy. Must be ≥ 0.
-
writeTo
public void writeTo(@Nonnull byte[] aBuf, @Nonnegative int nOfs, @Nonnegative int nLen, boolean bCompactBuffer) Write current content to the passed byte array. The copied elements are removed from this streams buffer.- Parameters:
aBuf- Byte array to write to. May not benull.nOfs- Offset to start writing. Must be ≥ 0.nLen- Number of bytes to copy. Must be ≥ 0.bCompactBuffer-trueto compact the buffer afterwards,falseotherwise.
-
writeTo
Write everything to the passed output stream and clear the contained buffer. This works only if the contained ByteBuffer has a backing array.- Specified by:
writeToin interfaceIWriteToStream- Parameters:
aOS- The output stream to write to. May not benull.- Throws:
IOException- In case of IO error
-
writeTo
public void writeTo(@Nonnull @WillNotClose OutputStream aOS, boolean bClearBuffer) throws IOException Write everything to the passed output stream and optionally clear the contained buffer. This works only if the contained ByteBuffer has a backing array.- Parameters:
aOS- The output stream to write to. May not benull.bClearBuffer-trueto clear the buffer,falseto not do it. Iffalseyou may callreset()to clear it manually afterwards.- Throws:
IOException- In case of IO error
-
getAsString
Get the content as a string without modifying the buffer. This works only if the contained ByteBuffer has a backing array.- Parameters:
aCharset- The charset to use. May not benull.- Returns:
- The String representation.
-
write
public void write(int b) - Specified by:
writein classOutputStream
-
write
- Overrides:
writein classOutputStream
-
write
Write the content from the passed byte buffer to this output stream.- Parameters:
aSrcBuffer- The buffer to use. May not benull.
-