Package com.helger.base.io.stream
Class ByteBufferOutputStream
java.lang.Object
java.io.OutputStream
com.helger.base.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 @NonNull [] aArray) Constructor with an existing byte array to wrap.ByteBufferOutputStream(byte @NonNull [] 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(@NonNull ByteBuffer aBuffer, boolean bCanGrow) Constructor -
Method Summary
Modifier and TypeMethodDescriptionbooleancanGrow()voidclose()byte @NonNull []Get everything as a big byte array, without altering the ByteBuffer.@NonNull NonBlockingByteArrayInputStreamgetAsByteArrayInputStream(boolean bCopyNeeded) Get the current content as aNonBlockingByteArrayInputStream.@NonNull StringgetAsString(@NonNull Charset aCharset) Get the content as a string without modifying the buffer.@NonNull ByteBuffervoidreset()Reset the backing byte bufferintsize()voidwrite(byte @NonNull [] aBuf, int nOfs, int nLen) voidwrite(int b) voidwrite(@NonNull ByteBuffer aSrcBuffer) Write the content from the passed byte buffer to this output stream.voidwriteTo(byte @NonNull [] aBuf) Writes the current content to the passed buffer.voidwriteTo(byte @NonNull [] aBuf, boolean bCompactBuffer) Writes the current content to the passed buffer.voidwriteTo(byte @NonNull [] aBuf, int nOfs, int nLen) Write current content to the passed byte array.voidwriteTo(byte @NonNull [] aBuf, int nOfs, int nLen, boolean bCompactBuffer) Write current content to the passed byte array.voidwriteTo(@NonNull OutputStream aOS) Write everything to the passed output stream and clear the contained buffer.voidwriteTo(@NonNull OutputStream aOS, boolean bClearBuffer) Write everything to the passed output stream and optionally clear the contained buffer.voidwriteTo(@NonNull ByteBuffer aDestBuffer) Write everything currently contained to the specified buffer.voidwriteTo(@NonNull 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.base.io.iface.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
public ByteBufferOutputStream(@Nonnegative int nBytes) Constructor for an output stream than can grow.- Parameters:
nBytes- The initial number of bytes the buffer has. Must be ≥ 0.
-
ByteBufferOutputStream
public ByteBufferOutputStream(@Nonnegative int nBytes, boolean bCanGrow) Constructor- Parameters:
nBytes- The number of bytes the buffer has initially. Must be ≥ 0.bCanGrow-trueif the buffer can grow,falseotherwise.
-
ByteBufferOutputStream
public ByteBufferOutputStream(byte @NonNull [] aArray) Constructor with an existing byte array to wrap. This output stream cannot grow!- Parameters:
aArray- The array to be backed by aByteBuffer.
-
ByteBufferOutputStream
public ByteBufferOutputStream(byte @NonNull [] aArray, @Nonnegative int nOfs, @Nonnegative int nLen) 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
@Nonnegative public int size()- Returns:
- The number of bytes currently in the buffer. Always ≥ 0.
-
getAsByteArray
@ReturnsMutableCopy public byte @NonNull [] 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
Get the current content as aNonBlockingByteArrayInputStream. This works only if the contained ByteBuffer has a backing array.- Parameters:
bCopyNeeded-trueif a copy of the underlying byte array is needed,falseif the original array can be reused.- Returns:
- A new
NonBlockingByteArrayInputStream. Nevernull.
-
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
public void writeTo(byte @NonNull [] aBuf) 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
public void writeTo(byte @NonNull [] aBuf, boolean bCompactBuffer) 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
public void writeTo(byte @NonNull [] aBuf, @Nonnegative int nOfs, @Nonnegative int nLen) 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(byte @NonNull [] 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(@WillNotClose @NonNull 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
public void write(byte @NonNull [] aBuf, @Nonnegative int nOfs, @Nonnegative int nLen) - 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.
-