Class RecyclingBytesStreamOutput

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class RecyclingBytesStreamOutput extends BytesStream
An in-memory StreamOutput which first fills the given byte[] and then allocates more space from the given BigArrays if needed. The idea is that you can use this for passing data to an API that requires a single byte[] (or a BytesRef) which you'd prefer to re-use if possible, avoiding excessive allocations, but which may not always be large enough.
  • Constructor Details

    • RecyclingBytesStreamOutput

      public RecyclingBytesStreamOutput(byte[] buffer, BigArrays bigArrays)
  • Method Details

    • writeByte

      public void writeByte(byte b)
      Description copied from class: StreamOutput
      Writes a single byte.
      Specified by:
      writeByte in class StreamOutput
    • writeBytes

      public void writeBytes(byte[] b, int offset, int length)
      Description copied from class: StreamOutput
      Writes an array of bytes.
      Specified by:
      writeBytes in class StreamOutput
      Parameters:
      b - the bytes to write
      offset - the offset in the byte array
      length - the number of bytes to write
    • flush

      public void flush()
      Description copied from class: StreamOutput
      Forces any buffered output to be written.
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in class StreamOutput
    • close

      public void close() throws IOException
      Description copied from class: StreamOutput
      Closes this stream to further operations.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class StreamOutput
      Throws:
      IOException
    • reset

      public void reset() throws IOException
      Specified by:
      reset in class StreamOutput
      Throws:
      IOException
    • toBytesRef

      public org.apache.lucene.util.BytesRef toBytesRef()
      Return the written bytes in a BytesRef, avoiding allocating a new byte[] if the original buffer was already large enough. If we allocate a new (larger) buffer here then callers should typically re-use it for subsequent streams.
    • bytes

      public BytesReference bytes()
      Specified by:
      bytes in class BytesStream