@Override public ByteBuffer asByteBuffer(int index, int length) { checkIndex(index, length); ByteBuffer duplicate = this.byteBuffer.duplicate(); // Explicit access via Buffer base type for compatibility // with covariant return type on JDK 9's ByteBuffer... Buffer buffer = duplicate; buffer.position(index); buffer.limit(index + length); return duplicate.slice(); }
@Override public ChannelBuffer copy(int index, int length) { ByteBuffer src; try { src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length); } catch (IllegalArgumentException e) { throw new IndexOutOfBoundsException(); } ByteBuffer dst = buffer.isDirect() ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length); dst.put(src); dst.clear(); return new ByteBufferBackedChannelBuffer(dst); }
private void reallocIfNeeded() { if (size == capacity) { // Double the capacity while it is "sufficiently small", and otherwise increase by 50%. int newLength = capacity <= 65536 ? capacity << 1 : capacity + capacity >> 1; ByteBuffer buffer = Buffer.allocateDirectWithNativeOrder(calculateBufferCapacity(newLength)); // Copy over the old content of the memory and reset the position as we always act on the buffer as if // the position was never increased. memory.position(0).limit(size); buffer.put(memory); buffer.position(0); Buffer.free(memory); memory = buffer; memoryAddress = Buffer.memoryAddress(buffer); capacity = newLength; } }
@Override public DefaultDataBuffer slice(int index, int length) { checkIndex(index, length); int oldPosition = this.byteBuffer.position(); // Explicit access via Buffer base type for compatibility // with covariant return type on JDK 9's ByteBuffer... Buffer buffer = this.byteBuffer; try { buffer.position(index); ByteBuffer slice = this.byteBuffer.slice(); // Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer ((Buffer) slice).limit(length); return new SlicedDefaultDataBuffer(slice, this.dataBufferFactory, length); } finally { buffer.position(oldPosition); } }
@SuppressWarnings("unused") // Supposedly called by Caliper public double timeFold(int reps) { final ByteBuffer buf = allocateEmptyHLLBuffer(targetIsDirect, alignTarget, 0); for (int k = 0; k < reps; ++k) { for (int i = 0; i < count; ++i) { final int pos = positions[i]; final int size = sizes[i]; HyperLogLogCollector.makeCollector( (ByteBuffer) buf.duplicate().position(0).limit( HyperLogLogCollector.getLatestNumBytesForDenseStorage() ) ).fold( HyperLogLogCollector.makeCollector( (ByteBuffer) chunk.duplicate().limit(pos + size).position(pos) ) ); } } return HyperLogLogCollector.makeCollector(buf.duplicate()).estimateCardinality(); }
@Override public ChannelBuffer copy(int index, int length) { ByteBuffer src; try { src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length); } catch (IllegalArgumentException e) { throw new IndexOutOfBoundsException(); } ByteBuffer dst = buffer.isDirect() ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length); dst.put(src); dst.clear(); return new ByteBufferBackedChannelBuffer(dst); }
@Test public void ofByteBuffer() { byte[] bytes = "Hello, World!".getBytes(Charsets.UTF_8); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); byteBuffer.position(2).limit(11); ByteString byteString = ByteString.of(byteBuffer); // Verify that the bytes were copied out. byteBuffer.put(4, (byte) 'a'); assertEquals("llo, Worl", byteString.utf8()); }
@Override public int setBytes(int index, FileChannel in, long position, int length) throws IOException { checkIndex(index, length); ByteBuffer tmpBuf = internalNioBuffer(); index = idx(index); tmpBuf.clear().position(index).limit(index + length); try { return in.read(tmpBuf, position); } catch (ClosedChannelException ignored) { return -1; } }
@Override public DefaultDataBuffer read(byte[] destination, int offset, int length) { Assert.notNull(destination, "Byte array must not be null"); assertIndex(this.readPosition <= this.writePosition - length, "readPosition %d and length %d should be smaller than writePosition %d", this.readPosition, length, this.writePosition); ByteBuffer tmp = this.byteBuffer.duplicate(); int limit = this.readPosition + length; ((Buffer) tmp).clear().position(this.readPosition).limit(limit); tmp.get(destination, offset, length); this.readPosition += length; return this; }
@Override protected void memoryCopy(ByteBuffer src, int srcOffset, ByteBuffer dst, int dstOffset, int length) { if (length == 0) { return; } if (HAS_UNSAFE) { PlatformDependent.copyMemory( PlatformDependent.directBufferAddress(src) + srcOffset, PlatformDependent.directBufferAddress(dst) + dstOffset, length); } else { // We must duplicate the NIO buffers because they may be accessed by other Netty buffers. src = src.duplicate(); dst = dst.duplicate(); src.position(srcOffset).limit(srcOffset + length); dst.position(dstOffset); dst.put(src); } } }