/** * We allocate a number of byte buffers as the capacity. In order not to out * of the array bounds for the last byte(see {@link ByteBufferArray#multiple}), * we will allocate one additional buffer with capacity 0; * @param capacity total size of the byte buffer array * @param allocator the ByteBufferAllocator that will create the buffers * @throws IOException throws IOException if there is an exception thrown by the allocator */ public ByteBufferArray(long capacity, ByteBufferAllocator allocator) throws IOException { this.bufferSize = DEFAULT_BUFFER_SIZE; if (this.bufferSize > (capacity / 16)) this.bufferSize = (int) roundUp(capacity / 16, 32768); this.bufferCount = (int) (roundUp(capacity, bufferSize) / bufferSize); LOG.info("Allocating buffers total=" + StringUtils.byteDesc(capacity) + ", sizePerBuffer=" + StringUtils.byteDesc(bufferSize) + ", count=" + bufferCount); buffers = new ByteBuffer[bufferCount + 1]; createBuffers(allocator); }
@Test public void testByteBufferCreation1() throws Exception { ByteBufferAllocator allocator = new ByteBufferAllocator() { @Override public ByteBuffer allocate(long size) throws IOException { return ByteBuffer.allocateDirect((int) size); } }; ByteBufferArray array = new DummyByteBufferArray(7 * 1024 * 1024, allocator); // overwrite array.bufferCount = 25; array.buffers = new ByteBuffer[array.bufferCount + 1]; array.createBuffers(allocator); for (int i = 0; i < array.buffers.length; i++) { if (i == array.buffers.length - 1) { assertEquals(0, array.buffers[i].capacity()); } else { assertEquals(458752, array.buffers[i].capacity()); } } }
/** * We allocate a number of byte buffers as the capacity. In order not to out * of the array bounds for the last byte(see {@link ByteBufferArray#multiple}), * we will allocate one additional buffer with capacity 0; * @param capacity total size of the byte buffer array * @param allocator the ByteBufferAllocator that will create the buffers * @throws IOException throws IOException if there is an exception thrown by the allocator */ public ByteBufferArray(long capacity, ByteBufferAllocator allocator) throws IOException { this.bufferSize = DEFAULT_BUFFER_SIZE; if (this.bufferSize > (capacity / 16)) this.bufferSize = (int) roundUp(capacity / 16, 32768); this.bufferCount = (int) (roundUp(capacity, bufferSize) / bufferSize); LOG.info("Allocating buffers total=" + StringUtils.byteDesc(capacity) + ", sizePerBuffer=" + StringUtils.byteDesc(bufferSize) + ", count=" + bufferCount); buffers = new ByteBuffer[bufferCount + 1]; createBuffers(allocator); }
/** * We allocate a number of byte buffers as the capacity. In order not to out * of the array bounds for the last byte(see {@link ByteBufferArray#multiple}), * we will allocate one additional buffer with capacity 0; * @param capacity total size of the byte buffer array * @param allocator the ByteBufferAllocator that will create the buffers * @throws IOException throws IOException if there is an exception thrown by the allocator */ public ByteBufferArray(long capacity, ByteBufferAllocator allocator) throws IOException { this.bufferSize = DEFAULT_BUFFER_SIZE; if (this.bufferSize > (capacity / 16)) this.bufferSize = (int) roundUp(capacity / 16, 32768); this.bufferCount = (int) (roundUp(capacity, bufferSize) / bufferSize); LOG.info("Allocating buffers total=" + StringUtils.byteDesc(capacity) + ", sizePerBuffer=" + StringUtils.byteDesc(bufferSize) + ", count=" + bufferCount); buffers = new ByteBuffer[bufferCount + 1]; createBuffers(allocator); }
@Test public void testByteBufferCreation1() throws Exception { ByteBufferAllocator allocator = new ByteBufferAllocator() { @Override public ByteBuffer allocate(long size) throws IOException { return ByteBuffer.allocateDirect((int) size); } }; ByteBufferArray array = new DummyByteBufferArray(7 * 1024 * 1024, allocator); // overwrite array.bufferCount = 25; array.buffers = new ByteBuffer[array.bufferCount + 1]; array.createBuffers(allocator); for (int i = 0; i < array.buffers.length; i++) { if (i == array.buffers.length - 1) { assertEquals(0, array.buffers[i].capacity()); } else { assertEquals(458752, array.buffers[i].capacity()); } } }
@Test public void testByteBufferCreation1() throws Exception { ByteBufferAllocator allocator = new ByteBufferAllocator() { @Override public ByteBuffer allocate(long size) throws IOException { return ByteBuffer.allocateDirect((int) size); } }; ByteBufferArray array = new DummyByteBufferArray(7 * 1024 * 1024, allocator); // overwrite array.bufferCount = 25; array.buffers = new ByteBuffer[array.bufferCount + 1]; array.createBuffers(allocator); for (int i = 0; i < array.buffers.length; i++) { if (i == array.buffers.length - 1) { assertEquals(0, array.buffers[i].capacity()); } else { assertEquals(458752, array.buffers[i].capacity()); } } }