Package org.apache.hadoop.io
Class DataOutputBuffer
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.DataOutputStream
org.apache.hadoop.io.DataOutputBuffer
- All Implemented Interfaces:
Closeable,DataOutput,Flushable,AutoCloseable
@LimitedPrivate({"HDFS","MapReduce"})
@Unstable
public class DataOutputBuffer
extends DataOutputStream
A reusable
DataOutput implementation that writes to an in-memory
buffer.
This saves memory over creating a new DataOutputStream and ByteArrayOutputStream each time data is written.
Typical usage is something like the following:
DataOutputBuffer buffer = new DataOutputBuffer();
while (... loop condition ...) {
buffer.reset();
... write buffer using DataOutput methods ...
byte[] data = buffer.getData();
int dataLength = buffer.getLength();
... write data to its ultimate destination ...
}
-
Field Summary
Fields inherited from class java.io.DataOutputStream
writtenFields 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()Resets the buffer to empty.voidWrites bytes from a DataInput directly into the buffer.voidwriteInt(int v, int offset) Overwrite an integer into the internal buffer.voidwriteTo(OutputStream out) Write to a file stream.Methods inherited from class java.io.DataOutputStream
flush, size, write, write, writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeDouble, writeFloat, writeInt, writeLong, writeShort, writeUTFMethods inherited from class java.io.FilterOutputStream
close, writeMethods inherited from class java.io.OutputStream
nullOutputStreamMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.io.DataOutput
write
-
Constructor Details
-
DataOutputBuffer
public DataOutputBuffer()Constructs a new empty buffer. -
DataOutputBuffer
public DataOutputBuffer(int size)
-
-
Method Details
-
getData
public byte[] getData()Returns the current contents of the buffer. Data is only valid togetLength().- Returns:
- data byte.
-
getLength
public int getLength()Returns the length of the valid data currently in the buffer.- Returns:
- length.
-
reset
Resets the buffer to empty.- Returns:
- DataOutputBuffer.
-
write
Writes bytes from a DataInput directly into the buffer.- Parameters:
in- data input.length- length.- Throws:
IOException- raised on errors performing I/O.
-
writeTo
Write to a file stream.- Parameters:
out- OutputStream.- Throws:
IOException- raised on errors performing I/O.
-
writeInt
Overwrite an integer into the internal buffer. Note that this call can only be used to overwrite existing data in the buffer, i.e., buffer#count cannot be increased, and DataOutputStream#written cannot be increased.- Parameters:
v- v.offset- offset.- Throws:
IOException- raised on errors performing I/O.
-