Class OutputBuffer

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

@LimitedPrivate({"HDFS","MapReduce"}) @Unstable public class OutputBuffer extends FilterOutputStream
A reusable OutputStream implementation that writes to an in-memory buffer.

This saves memory over creating a new OutputStream and ByteArrayOutputStream each time data is written.

Typical usage is something like the following:


 OutputBuffer buffer = new OutputBuffer();
 while (... loop condition ...) {
   buffer.reset();
   ... write buffer using OutputStream methods ...
   byte[] data = buffer.getData();
   int dataLength = buffer.getLength();
   ... write data to its ultimate destination ...
 }
 
See Also:
  • Constructor Details

    • OutputBuffer

      public OutputBuffer()
      Constructs a new empty buffer.
  • Method Details

    • getData

      public byte[] getData()
      Returns the current contents of the buffer. Data is only valid to getLength().
      Returns:
      the current contents of the buffer.
    • getLength

      public int getLength()
      Returns the length of the valid data currently in the buffer.
      Returns:
      the length of the valid data currently in the buffer.
    • reset

      public OutputBuffer reset()
      Returns:
      Resets the buffer to empty.
    • write

      public void write(InputStream in, int length) throws IOException
      Writes bytes from a InputStream directly into the buffer.
      Parameters:
      in - input in.
      length - input length.
      Throws:
      IOException - raised on errors performing I/O.