/** Resizes the internal byte buffer with a simple doubling policy, if needed. */ private final void growIfNeeded(int minimumDesired) { if (buffer.b.remaining() < minimumDesired) { // Compute the size of the new buffer int newCapacity = buffer.b.capacity(); int newRemaining = newCapacity - buffer.b.position(); while (newRemaining < minimumDesired) { newRemaining += newCapacity; newCapacity *= 2; } // Allocate and copy BBContainer next; if (isDirect) { next = DBBPool.allocateDirect(newCapacity); } else if (m_pool != null) { next = m_pool.acquire(newCapacity); } else { next = DBBPool.wrapBB(ByteBuffer.allocate(newCapacity)); } buffer.b.flip(); next.b.put(buffer.b); assert next.b.remaining() == newRemaining; buffer.discard(); buffer = next; if (callback != null) callback.onBufferGrow(this); assert (buffer.b.order() == ByteOrder.BIG_ENDIAN); } }
/** constructor that sets callback object. */ public FastSerializer( boolean bigEndian, boolean isDirect, BufferGrowCallback callback, DBBPool pool, int initialAllocation) { assert (initialAllocation > 0); assert (pool == null && isDirect || pool != null && !isDirect || pool == null && !isDirect); this.isDirect = isDirect; if (pool != null) { m_pool = pool; buffer = pool.acquire(initialAllocation); } else if (isDirect) { assert (pool == null); m_pool = null; buffer = DBBPool.allocateDirect(initialAllocation); } else { buffer = DBBPool.wrapBB(ByteBuffer.allocate(initialAllocation)); m_pool = null; assert (pool == null); } this.callback = callback; buffer.b.order(bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN); }