com.duowan.mobile.netroid.toolbox
Class PoolingByteArrayOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.ByteArrayOutputStream
          extended by com.duowan.mobile.netroid.toolbox.PoolingByteArrayOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class PoolingByteArrayOutputStream
extends java.io.ByteArrayOutputStream

A variation of ByteArrayOutputStream that uses a pool of byte[] buffers instead of always allocating them fresh, saving on heap churn.


Field Summary
 
Fields inherited from class java.io.ByteArrayOutputStream
buf, count
 
Constructor Summary
PoolingByteArrayOutputStream(ByteArrayPool pool)
          Constructs a new PoolingByteArrayOutputStream with a default size.
PoolingByteArrayOutputStream(ByteArrayPool pool, int size)
          Constructs a new ByteArrayOutputStream with a default size of size bytes.
 
Method Summary
 void close()
           
 void finalize()
           
 void write(byte[] buffer, int offset, int len)
           
 void write(int oneByte)
           
 
Methods inherited from class java.io.ByteArrayOutputStream
reset, size, toByteArray, toString, toString, toString, writeTo
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PoolingByteArrayOutputStream

public PoolingByteArrayOutputStream(ByteArrayPool pool)
Constructs a new PoolingByteArrayOutputStream with a default size. If more bytes are written to this instance, the underlying byte array will expand.


PoolingByteArrayOutputStream

public PoolingByteArrayOutputStream(ByteArrayPool pool,
                                    int size)
Constructs a new ByteArrayOutputStream with a default size of size bytes. If more than size bytes are written to this instance, the underlying byte array will expand.

Parameters:
size - initial size for the underlying byte array. The value will be pinned to a default minimum size.
Method Detail

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.ByteArrayOutputStream
Throws:
java.io.IOException

finalize

public void finalize()
Overrides:
finalize in class java.lang.Object

write

public void write(byte[] buffer,
                  int offset,
                  int len)
Overrides:
write in class java.io.ByteArrayOutputStream

write

public void write(int oneByte)
Overrides:
write in class java.io.ByteArrayOutputStream