### Question:
ByteBufferOutputStream extends OutputStream { private void grow(int minCapacity) { if (buffer.capacity() >= minCapacity) { buffer.limit(minCapacity); return; } int oldCapacity = buffer.capacity(); int newCapacity = oldCapacity << 1; if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity < 0) { if (minCapacity < 0) throw new OutOfMemoryError("Math overflow!"); newCapacity = Integer.MAX_VALUE; } assert newCapacity >= minCapacity : "Math is hard"; int position = buffer.position(); ByteBuffer oldBuffer = buffer; buffer = bufferPool.allocate(newCapacity); buffer.put(oldBuffer.array(), 0, position); bufferPool.free(oldBuffer); assert buffer.capacity() >= minCapacity : "Math is hard"; } ByteBufferOutputStream(ByteBufferPool bp);  ByteBufferOutputStream(ByteBufferPool bp, int initialSize); ByteBuffer toByteBuffer(); void write(int b); void write(byte b[], int off, int len); void reset(); int size(); void close();  }### Answer:
@Test public void testGrow() { ByteBufferPool pool = new ByteBufferPool("test", 100); ByteBufferOutputStream test = new ByteBufferOutputStream(pool); for (int i = 0; i < 1024 * 1024; i++) { test.write(5); } ByteBuffer produced = test.toByteBuffer(); assertEquals("Invalid buffer limit", 1024 * 1024, produced.limit()); assertEquals("Invalid capacity", 1048576, produced.capacity()); assertEquals("Invalid bytes allocated", 2097120, pool.getBytesAllocated()); assertEquals("Invalid buffer allocations", 16, pool.getCreated()); }