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 an OutputStream around a ByteBuffer.
Author:
Philip Helger
  • Field Details

  • Constructor Details

    • ByteBufferOutputStream

      public ByteBufferOutputStream()
      Create a new object with the DEFAULT_BUF_SIZE buffer 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 - true if the buffer can grow, false otherwise.
    • 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 a ByteBuffer.
    • 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 a ByteBuffer.
      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 be null.
      bCanGrow - true if the buffer can grow, false otherwise.
  • Method Details

    • getBuffer

      @Nonnull public ByteBuffer getBuffer()
      Returns:
      The contained buffer. Never null.
    • canGrow

      public boolean canGrow()
      Returns:
      true if this buffer can grow, false if not.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
    • 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, a BufferOverflowException is thrown. The copied elements are removed from this streams buffer.
      Parameters:
      aDestBuffer - The destination buffer to write to. May not be null.
    • writeTo

      public void writeTo(@Nonnull ByteBuffer aDestBuffer, boolean bCompactBuffer)
      Write everything currently contained to the specified buffer. If the passed buffer is too small, a BufferOverflowException is thrown. The copied elements are removed from this streams buffer.
      Parameters:
      aDestBuffer - The destination buffer to write to. May not be null.
      bCompactBuffer - true to compact the buffer afterwards, false otherwise.
    • 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 be null.
    • 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 be null.
      bCompactBuffer - true to compact the buffer afterwards, false otherwise.
    • 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 be null.
      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 be null.
      nOfs - Offset to start writing. Must be ≥ 0.
      nLen - Number of bytes to copy. Must be ≥ 0.
      bCompactBuffer - true to compact the buffer afterwards, false otherwise.
    • 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:
      writeTo in interface IWriteToStream
      Parameters:
      aOS - The output stream to write to. May not be null.
      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 be null.
      bClearBuffer - true to clear the buffer, false to not do it. If false you may call reset() 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 be null.
      Returns:
      The String representation.
    • write

      public void write(int b)
      Specified by:
      write in class OutputStream
    • write

      public void write(@Nonnull byte[] aBuf, @Nonnegative int nOfs, @Nonnegative int nLen)
      Overrides:
      write in class OutputStream
    • 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 be null.