PoolingByteArrayOutputStreampublic 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_SIZEIf 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.
mPool = pool;
buf = mPool.getBuf(Math.max(size, DEFAULT_SIZE));
|
Methods Summary |
---|
public void | close()
mPool.returnBuf(buf);
buf = null;
super.close();
| private void | expand(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 void | finalize()
mPool.returnBuf(buf);
| public synchronized void | write(byte[] buffer, int offset, int len)
expand(len);
super.write(buffer, offset, len);
| public synchronized void | write(int oneByte)
expand(1);
super.write(oneByte);
|
|