FileDocCategorySizeDatePackage
PoolingByteArrayOutputStream.javaAPI DocAndroid 5.1 API2991Thu Mar 12 22:22:56 GMT 2015com.android.volley.toolbox

PoolingByteArrayOutputStream

public class PoolingByteArrayOutputStream extends ByteArrayOutputStream
A variation of {@link java.io.ByteArrayOutputStream} that uses a pool of byte[] buffers instead of always allocating them fresh, saving on heap churn.

Fields Summary
private static final int
DEFAULT_SIZE
If the {@link #PoolingByteArrayOutputStream(ByteArrayPool)} constructor is called, this is the default size to which the underlying byte array is initialized.
private final ByteArrayPool
mPool
Constructors Summary
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.


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

param
size initial size for the underlying byte array. The value will be pinned to a default minimum size.

        mPool = pool;
        buf = mPool.getBuf(Math.max(size, DEFAULT_SIZE));
    
Methods Summary
public voidclose()

        mPool.returnBuf(buf);
        buf = null;
        super.close();
    
private voidexpand(int i)
Ensures there is enough space in the buffer for the given number of additional bytes.

        /* Can the buffer handle @i more bytes, if not expand it */
        if (count + i <= buf.length) {
            return;
        }
        byte[] newbuf = mPool.getBuf((count + i) * 2);
        System.arraycopy(buf, 0, newbuf, 0, count);
        mPool.returnBuf(buf);
        buf = newbuf;
    
public voidfinalize()

        mPool.returnBuf(buf);
    
public synchronized voidwrite(byte[] buffer, int offset, int len)

        expand(len);
        super.write(buffer, offset, len);
    
public synchronized voidwrite(int oneByte)

        expand(1);
        super.write(oneByte);