Class ByteBufferOutputStream2

java.lang.Object
java.io.OutputStream
org.eclipse.jetty.io.ByteBufferOutputStream2
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class ByteBufferOutputStream2 extends OutputStream
This class implements an output stream in which the data is written into a list of ByteBuffer, the buffer list automatically grows as data is written to it, the buffers are taken from the supplied ByteBufferPool or freshly allocated if one is not supplied. Designed to mimic ByteArrayOutputStream but with better memory usage, and less copying.
  • Constructor Details

    • ByteBufferOutputStream2

      public ByteBufferOutputStream2()
    • ByteBufferOutputStream2

      public ByteBufferOutputStream2(ByteBufferPool bufferPool, boolean direct)
  • Method Details

    • takeByteBuffer

      public RetainableByteBuffer takeByteBuffer()
      Take the combined buffer containing all content written to the OutputStream. The caller is responsible for releasing this RetainableByteBuffer.
      Returns:
      a buffer containing all content written to the OutputStream.
    • toByteBuffer

      public RetainableByteBuffer toByteBuffer()
      Take the combined buffer containing all content written to the OutputStream. The returned buffer is still contained within the OutputStream and will be released when the OutputStream is closed.
      Returns:
      a buffer containing all content written to the OutputStream.
    • toByteArray

      public byte[] toByteArray()
      Returns:
      a newly allocated byte array containing all content written into the OutputStream.
    • size

      public int size()
    • write

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

      public void write(byte[] b, int off, int len)
      Overrides:
      write in class OutputStream
    • write

      public void write(ByteBuffer buffer)
    • writeTo

      public void writeTo(ByteBuffer buffer)
    • writeTo

      public void writeTo(OutputStream out) throws IOException
      Throws:
      IOException
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
    • toString

      public String toString()
      Overrides:
      toString in class Object