org.apache.pig.data
Class WritableByteArray

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.ByteArrayOutputStream
          extended by org.apache.pig.data.WritableByteArray
All Implemented Interfaces:
Closeable, Flushable

public class WritableByteArray
extends ByteArrayOutputStream

A reusable byte buffer implementation

This saves memory over creating a new byte[] and ByteArrayOutputStream each time data is written.

Typical usage is something like the following:


 WritableByteArray buffer = new WritableByteArray();
 while (... loop condition ...) {
   buffer.reset();
   ... write to buffer ...
   byte[] data = buffer.getData();
   int dataLength = buffer.getLength();
   ... write data to its ultimate destination ...
 }
 


Field Summary
 
Fields inherited from class java.io.ByteArrayOutputStream
buf, count
 
Constructor Summary
WritableByteArray()
           
WritableByteArray(byte[] buf)
           
WritableByteArray(int size)
           
 
Method Summary
 byte[] getData()
          Returns the current contents of the buffer.
 int getLength()
          Returns the length of the valid data currently in the buffer
 
Methods inherited from class java.io.ByteArrayOutputStream
close, reset, size, toByteArray, toString, toString, toString, write, write, writeTo
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

WritableByteArray

public WritableByteArray()

WritableByteArray

public WritableByteArray(int size)

WritableByteArray

public WritableByteArray(byte[] buf)
Method Detail

getData

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

Returns:
contents of the buffer

getLength

public int getLength()
Returns the length of the valid data currently in the buffer

Returns:
length of valid data


Copyright © 2007-2012 The Apache Software Foundation