@Override public ByteBuf readBytes(int length) { checkReadableBytes(length); if (length == 0) { return Unpooled.EMPTY_BUFFER; } ByteBuf buf = alloc().buffer(length, maxCapacity); buf.writeBytes(this, readerIndex, length); readerIndex += length; return buf; }
@Override public final ByteBufAllocator alloc() { return unwrap().alloc(); }
@Override public ByteBuf readBytes(int length) { checkReadableBytes(length); if (length == 0) { return Unpooled.EMPTY_BUFFER; } ByteBuf buf = alloc().buffer(length, maxCapacity); buf.writeBytes(this, readerIndex, length); readerIndex += length; return buf; }
@Override public final ByteBufAllocator alloc() { return unwrap().alloc(); }
@Override public ByteBuf readBytes(int length) { checkReadableBytes(length); if (length == 0) { return Unpooled.EMPTY_BUFFER; } ByteBuf buf = alloc().buffer(length, maxCapacity); buf.writeBytes(this, readerIndex, length); readerIndex += length; return buf; }
@Override public final ByteBufAllocator alloc() { return unwrap().alloc(); }
static int setBytes(AbstractByteBuf buf, long addr, int index, InputStream in, int length) throws IOException { buf.checkIndex(index, length); ByteBuf tmpBuf = buf.alloc().heapBuffer(length); try { byte[] tmp = tmpBuf.array(); int offset = tmpBuf.arrayOffset(); int readBytes = in.read(tmp, offset, length); if (readBytes > 0) { PlatformDependent.copyMemory(tmp, offset, addr, readBytes); } return readBytes; } finally { tmpBuf.release(); } }
final void ensureWritable0(int minWritableBytes) { ensureAccessible(); if (minWritableBytes <= writableBytes()) { return; } if (checkBounds) { if (minWritableBytes > maxCapacity - writerIndex) { throw new IndexOutOfBoundsException(String.format( "writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s", writerIndex, minWritableBytes, maxCapacity, this)); } } // Normalize the current capacity to the power of 2. int newCapacity = alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity); // Adjust to the new capacity. capacity(newCapacity); }
static int setBytes(AbstractByteBuf buf, long addr, int index, InputStream in, int length) throws IOException { buf.checkIndex(index, length); ByteBuf tmpBuf = buf.alloc().heapBuffer(length); try { byte[] tmp = tmpBuf.array(); int offset = tmpBuf.arrayOffset(); int readBytes = in.read(tmp, offset, length); if (readBytes > 0) { PlatformDependent.copyMemory(tmp, offset, addr, readBytes); } return readBytes; } finally { tmpBuf.release(); } }
static void getBytes(AbstractByteBuf buf, long addr, int index, OutputStream out, int length) throws IOException { buf.checkIndex(index, length); if (length != 0) { int len = Math.min(length, ByteBufUtil.WRITE_CHUNK_SIZE); if (len <= ByteBufUtil.MAX_TL_ARRAY_LEN || !buf.alloc().isDirectBufferPooled()) { getBytes(addr, ByteBufUtil.threadLocalTempArray(len), 0, len, out, length); } else { // if direct buffers are pooled chances are good that heap buffers are pooled as well. ByteBuf tmpBuf = buf.alloc().heapBuffer(len); try { byte[] tmp = tmpBuf.array(); int offset = tmpBuf.arrayOffset(); getBytes(addr, tmp, offset, len, out, length); } finally { tmpBuf.release(); } } } }
final void ensureWritable0(int minWritableBytes) { ensureAccessible(); if (minWritableBytes <= writableBytes()) { return; } if (checkBounds) { if (minWritableBytes > maxCapacity - writerIndex) { throw new IndexOutOfBoundsException(String.format( "writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s", writerIndex, minWritableBytes, maxCapacity, this)); } } // Normalize the current capacity to the power of 2. int newCapacity = alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity); // Adjust to the new capacity. capacity(newCapacity); }
static void getBytes(AbstractByteBuf buf, long addr, int index, OutputStream out, int length) throws IOException { buf.checkIndex(index, length); if (length != 0) { int len = Math.min(length, ByteBufUtil.WRITE_CHUNK_SIZE); if (len <= ByteBufUtil.MAX_TL_ARRAY_LEN || !buf.alloc().isDirectBufferPooled()) { getBytes(addr, ByteBufUtil.threadLocalTempArray(len), 0, len, out, length); } else { // if direct buffers are pooled chances are good that heap buffers are pooled as well. ByteBuf tmpBuf = buf.alloc().heapBuffer(len); try { byte[] tmp = tmpBuf.array(); int offset = tmpBuf.arrayOffset(); getBytes(addr, tmp, offset, len, out, length); } finally { tmpBuf.release(); } } } }
static int setBytes(AbstractByteBuf buf, long addr, int index, InputStream in, int length) throws IOException { buf.checkIndex(index, length); ByteBuf tmpBuf = buf.alloc().heapBuffer(length); try { byte[] tmp = tmpBuf.array(); int offset = tmpBuf.arrayOffset(); int readBytes = in.read(tmp, offset, length); if (readBytes > 0) { PlatformDependent.copyMemory(tmp, offset, addr, readBytes); } return readBytes; } finally { tmpBuf.release(); } }
final void ensureWritable0(int minWritableBytes) { ensureAccessible(); if (minWritableBytes <= writableBytes()) { return; } if (minWritableBytes > maxCapacity - writerIndex) { throw new IndexOutOfBoundsException(String.format( "writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s", writerIndex, minWritableBytes, maxCapacity, this)); } // Normalize the current capacity to the power of 2. int newCapacity = alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity); // Adjust to the new capacity. capacity(newCapacity); }
static void getBytes(AbstractByteBuf buf, long addr, int index, OutputStream out, int length) throws IOException { buf.checkIndex(index, length); if (length != 0) { int len = Math.min(length, ByteBufUtil.WRITE_CHUNK_SIZE); if (buf.alloc().isDirectBufferPooled()) { // if direct buffers are pooled chances are good that heap buffers are pooled as well. ByteBuf tmpBuf = buf.alloc().heapBuffer(len); try { byte[] tmp = tmpBuf.array(); int offset = tmpBuf.arrayOffset(); getBytes(addr, tmp, offset, len, out, length); } finally { tmpBuf.release(); } } else { getBytes(addr, new byte[len], 0, len, out, length); } } }
static ByteBuf copy(AbstractByteBuf buf, long addr, int index, int length) { buf.checkIndex(index, length); ByteBuf copy = buf.alloc().directBuffer(length, buf.maxCapacity()); if (length != 0) { if (copy.hasMemoryAddress()) { PlatformDependent.copyMemory(addr, copy.memoryAddress(), length); copy.setIndex(0, length); } else { copy.writeBytes(buf, index, length); } } return copy; }
static ByteBuf copy(AbstractByteBuf buf, long addr, int index, int length) { buf.checkIndex(index, length); ByteBuf copy = buf.alloc().directBuffer(length, buf.maxCapacity()); if (length != 0) { if (copy.hasMemoryAddress()) { PlatformDependent.copyMemory(addr, copy.memoryAddress(), length); copy.setIndex(0, length); } else { copy.writeBytes(buf, index, length); } } return copy; }
@Override public int ensureWritable(int minWritableBytes, boolean force) { ensureAccessible(); if (minWritableBytes < 0) { throw new IllegalArgumentException(String.format( "minWritableBytes: %d (expected: >= 0)", minWritableBytes)); } if (minWritableBytes <= writableBytes()) { return 0; } final int maxCapacity = maxCapacity(); final int writerIndex = writerIndex(); if (minWritableBytes > maxCapacity - writerIndex) { if (!force || capacity() == maxCapacity) { return 1; } capacity(maxCapacity); return 3; } // Normalize the current capacity to the power of 2. int newCapacity = alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity); // Adjust to the new capacity. capacity(newCapacity); return 2; }
static ByteBuf copy(AbstractByteBuf buf, long addr, int index, int length) { buf.checkIndex(index, length); ByteBuf copy = buf.alloc().directBuffer(length, buf.maxCapacity()); if (length != 0) { if (copy.hasMemoryAddress()) { PlatformDependent.copyMemory(addr, copy.memoryAddress(), length); copy.setIndex(0, length); } else { copy.writeBytes(buf, index, length); } } return copy; }
@Override public int ensureWritable(int minWritableBytes, boolean force) { ensureAccessible(); checkPositiveOrZero(minWritableBytes, "minWritableBytes"); if (minWritableBytes <= writableBytes()) { return 0; } final int maxCapacity = maxCapacity(); final int writerIndex = writerIndex(); if (minWritableBytes > maxCapacity - writerIndex) { if (!force || capacity() == maxCapacity) { return 1; } capacity(maxCapacity); return 3; } // Normalize the current capacity to the power of 2. int newCapacity = alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity); // Adjust to the new capacity. capacity(newCapacity); return 2; }