/** * @return Returns a new {@link ByteBuffersDataOutput} with the {@link #reset()} capability. */ // TODO: perhaps we can move it out to an utility class (as a supplier of preconfigured instances?) public static ByteBuffersDataOutput newResettableInstance() { ByteBuffersDataOutput.ByteBufferRecycler reuser = new ByteBuffersDataOutput.ByteBufferRecycler( ByteBuffersDataOutput.ALLOCATE_BB_ON_HEAP); return new ByteBuffersDataOutput( ByteBuffersDataOutput.DEFAULT_MIN_BITS_PER_BLOCK, ByteBuffersDataOutput.DEFAULT_MAX_BITS_PER_BLOCK, reuser::allocate, reuser::reuse); }
private void rewriteToBlockSize(int targetBlockBits) { assert targetBlockBits <= maxBitsPerBlock; // We copy over data blocks to an output with one-larger block bit size. // We also discard references to blocks as we're copying to allow GC to // clean up partial results in case of memory pressure. ByteBuffersDataOutput cloned = new ByteBuffersDataOutput(targetBlockBits, targetBlockBits, blockAllocate, NO_REUSE); ByteBuffer block; while ((block = blocks.pollFirst()) != null) { block.flip(); cloned.writeBytes(block); if (blockReuse != NO_REUSE) { blockReuse.accept(block); } } assert blocks.isEmpty(); this.blockBits = targetBlockBits; blocks.addAll(cloned.blocks); }
/** * @return Returns a new {@link ByteBuffersDataOutput} with the {@link #reset()} capability. */ // TODO: perhaps we can move it out to an utility class (as a supplier of preconfigured instances?) public static ByteBuffersDataOutput newResettableInstance() { ByteBuffersDataOutput.ByteBufferRecycler reuser = new ByteBuffersDataOutput.ByteBufferRecycler( ByteBuffersDataOutput.ALLOCATE_BB_ON_HEAP); return new ByteBuffersDataOutput( ByteBuffersDataOutput.DEFAULT_MIN_BITS_PER_BLOCK, ByteBuffersDataOutput.DEFAULT_MAX_BITS_PER_BLOCK, reuser::allocate, reuser::reuse); }
private void rewriteToBlockSize(int targetBlockBits) { assert targetBlockBits <= maxBitsPerBlock; // We copy over data blocks to an output with one-larger block bit size. // We also discard references to blocks as we're copying to allow GC to // clean up partial results in case of memory pressure. ByteBuffersDataOutput cloned = new ByteBuffersDataOutput(targetBlockBits, targetBlockBits, blockAllocate, NO_REUSE); ByteBuffer block; while ((block = blocks.pollFirst()) != null) { block.flip(); cloned.writeBytes(block); if (blockReuse != NO_REUSE) { blockReuse.accept(block); } } assert blocks.isEmpty(); this.blockBits = targetBlockBits; blocks.addAll(cloned.blocks); }