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
public class ByteBufferOutputStream extends OutputStream implements IWriteToStream
Wrapper for anOutputStreamaround aByteBuffer.- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_BUF_SIZEstatic booleanDEFAULT_CAN_GROW
-
Constructor Summary
Constructors Constructor Description ByteBufferOutputStream()Create 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanGrow()voidclose()byte[]getAsByteArray()Get everything as a big byte array, without altering the ByteBuffer.NonBlockingByteArrayInputStreamgetAsByteArrayInputStream(boolean bCopyNeeded)ByteArrayWrappergetAsByteArrayWrapper(boolean bCopyNeeded)StringgetAsString(Charset aCharset)Get the content as a string without modifying the buffer.ByteBuffergetBuffer()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, write
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.helger.commons.io.IWriteToStream
writeToAndClose
-
-
-
-
Field Detail
-
DEFAULT_BUF_SIZE
public static final int DEFAULT_BUF_SIZE
- See Also:
- Constant Field Values
-
DEFAULT_CAN_GROW
public static final boolean DEFAULT_CAN_GROW
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
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(@Nonnull byte[] 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(@Nonnull byte[] 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
public ByteBufferOutputStream(@Nonnull ByteBuffer aBuffer, boolean bCanGrow)
Constructor- Parameters:
aBuffer- The byte buffer to use. May not benull.bCanGrow-trueif the buffer can grow,falseotherwise.
-
-
Method Detail
-
getBuffer
@Nonnull public ByteBuffer 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
@Nonnull @ReturnsMutableCopy public byte[] 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
@Nonnull public NonBlockingByteArrayInputStream getAsByteArrayInputStream(boolean bCopyNeeded)
-
getAsByteArrayWrapper
@Nonnull public ByteArrayWrapper getAsByteArrayWrapper(boolean bCopyNeeded)
-
writeTo
public void writeTo(@Nonnull ByteBuffer aDestBuffer)
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
public void writeTo(@Nonnull ByteBuffer aDestBuffer, boolean bCompactBuffer)
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(@Nonnull byte[] 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(@Nonnull byte[] 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(@Nonnull byte[] 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(@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
public void writeTo(@Nonnull @WillNotClose OutputStream aOS) throws IOException
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
@Nonnull public String getAsString(@Nonnull Charset aCharset)
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(@Nonnull byte[] aBuf, @Nonnegative int nOfs, @Nonnegative int nLen)
- Overrides:
writein classOutputStream
-
write
public void write(@Nonnull ByteBuffer aSrcBuffer)
Write the content from the passed byte buffer to this output stream.- Parameters:
aSrcBuffer- The buffer to use. May not benull.
-
-