Package org.apache.hadoop.io
Class OutputBuffer
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
org.apache.hadoop.io.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:
-
Field Summary
Fields inherited from class java.io.FilterOutputStream
out -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbyte[]getData()Returns the current contents of the buffer.intReturns the length of the valid data currently in the buffer.reset()voidwrite(InputStream in, int length) Writes bytes from a InputStream directly into the buffer.Methods inherited from class java.io.OutputStream
nullOutputStream
-
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 togetLength().- 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
- Returns:
- Resets the buffer to empty.
-
write
Writes bytes from a InputStream directly into the buffer.- Parameters:
in- input in.length- input length.- Throws:
IOException- raised on errors performing I/O.
-