@Override public int setBytes(int index, InputStream in, int length) throws IOException { ensureAccessible(); if (buffer.hasArray()) { return in.read(buffer.array(), buffer.arrayOffset() + index, length); } else { byte[] tmp = ByteBufUtil.threadLocalTempArray(length); int readBytes = in.read(tmp, 0, length); if (readBytes <= 0) { return readBytes; } ByteBuffer tmpBuf = internalNioBuffer(); tmpBuf.clear().position(index); tmpBuf.put(tmp, 0, readBytes); return readBytes; } }
@Override public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { ensureAccessible(); if (length == 0) { return this; } if (buffer.hasArray()) { out.write(buffer.array(), index + buffer.arrayOffset(), length); } else { byte[] tmp = ByteBufUtil.threadLocalTempArray(length); ByteBuffer tmpBuf = internalNioBuffer(); tmpBuf.clear().position(index); tmpBuf.get(tmp, 0, length); out.write(tmp, 0, length); } return this; }
@Override public int setBytes(int index, InputStream in, int length) throws IOException { checkIndex(index, length); byte[] tmp = ByteBufUtil.threadLocalTempArray(length); int readBytes = in.read(tmp, 0, length); if (readBytes <= 0) { return readBytes; } ByteBuffer tmpBuf = internalNioBuffer(); tmpBuf.clear().position(idx(index)); tmpBuf.put(tmp, 0, readBytes); return readBytes; }
@Override public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { ensureAccessible(); if (length == 0) { return this; } if (buffer.hasArray()) { out.write(buffer.array(), index + buffer.arrayOffset(), length); } else { byte[] tmp = ByteBufUtil.threadLocalTempArray(length); ByteBuffer tmpBuf = internalNioBuffer(); tmpBuf.clear().position(index); tmpBuf.get(tmp, 0, length); out.write(tmp, 0, length); } return this; }
@Override public int setBytes(int index, InputStream in, int length) throws IOException { ensureAccessible(); if (buffer.hasArray()) { return in.read(buffer.array(), buffer.arrayOffset() + index, length); } else { byte[] tmp = ByteBufUtil.threadLocalTempArray(length); int readBytes = in.read(tmp, 0, length); if (readBytes <= 0) { return readBytes; } ByteBuffer tmpBuf = internalNioBuffer(); tmpBuf.clear().position(index); tmpBuf.put(tmp, 0, readBytes); return readBytes; } }
@Override public int setBytes(int index, InputStream in, int length) throws IOException { checkIndex(index, length); byte[] tmp = ByteBufUtil.threadLocalTempArray(length); int readBytes = in.read(tmp, 0, length); if (readBytes <= 0) { return readBytes; } ByteBuffer tmpBuf = internalNioBuffer(); tmpBuf.clear().position(idx(index)); tmpBuf.put(tmp, 0, readBytes); return readBytes; }
@SuppressWarnings("deprecation") static String decodeString(ByteBuf src, int readerIndex, int len, Charset charset) { if (len == 0) { return StringUtil.EMPTY_STRING; } final byte[] array; final int offset; if (src.hasArray()) { array = src.array(); offset = src.arrayOffset() + readerIndex; } else { array = threadLocalTempArray(len); offset = 0; src.getBytes(readerIndex, array, 0, len); } if (CharsetUtil.US_ASCII.equals(charset)) { // Fast-path for US-ASCII which is used frequently. return new String(array, 0, offset, len); } return new String(array, offset, len, charset); }
/** * Read bytes from the given {@link ByteBuffer} into the given {@link OutputStream} using the {@code position} and * {@code length}. The position and limit of the given {@link ByteBuffer} may be adjusted. */ static void readBytes(ByteBufAllocator allocator, ByteBuffer buffer, int position, int length, OutputStream out) throws IOException { if (buffer.hasArray()) { out.write(buffer.array(), position + buffer.arrayOffset(), length); } else { int chunkLen = Math.min(length, WRITE_CHUNK_SIZE); buffer.clear().position(position); if (length <= MAX_TL_ARRAY_LEN || !allocator.isDirectBufferPooled()) { getBytes(buffer, threadLocalTempArray(chunkLen), 0, chunkLen, out, length); } else { // if direct buffers are pooled chances are good that heap buffers are pooled as well. ByteBuf tmpBuf = allocator.heapBuffer(chunkLen); try { byte[] tmp = tmpBuf.array(); int offset = tmpBuf.arrayOffset(); getBytes(buffer, tmp, offset, chunkLen, out, length); } finally { tmpBuf.release(); } } } }
@SuppressWarnings("deprecation") static String decodeString(ByteBuf src, int readerIndex, int len, Charset charset) { if (len == 0) { return StringUtil.EMPTY_STRING; } final byte[] array; final int offset; if (src.hasArray()) { array = src.array(); offset = src.arrayOffset() + readerIndex; } else { array = threadLocalTempArray(len); offset = 0; src.getBytes(readerIndex, array, 0, len); } if (CharsetUtil.US_ASCII.equals(charset)) { // Fast-path for US-ASCII which is used frequently. return new String(array, 0, offset, len); } return new String(array, offset, len, charset); }
/** * Read bytes from the given {@link ByteBuffer} into the given {@link OutputStream} using the {@code position} and * {@code length}. The position and limit of the given {@link ByteBuffer} may be adjusted. */ static void readBytes(ByteBufAllocator allocator, ByteBuffer buffer, int position, int length, OutputStream out) throws IOException { if (buffer.hasArray()) { out.write(buffer.array(), position + buffer.arrayOffset(), length); } else { int chunkLen = Math.min(length, WRITE_CHUNK_SIZE); buffer.clear().position(position); if (length <= MAX_TL_ARRAY_LEN || !allocator.isDirectBufferPooled()) { getBytes(buffer, threadLocalTempArray(chunkLen), 0, chunkLen, out, length); } else { // if direct buffers are pooled chances are good that heap buffers are pooled as well. ByteBuf tmpBuf = allocator.heapBuffer(chunkLen); try { byte[] tmp = tmpBuf.array(); int offset = tmpBuf.arrayOffset(); getBytes(buffer, tmp, offset, chunkLen, out, length); } 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(); } } } }
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(); } } } }
@Override public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { ensureAccessible(); if (length == 0) { return this; } if (buffer.hasArray()) { out.write(buffer.array(), index + buffer.arrayOffset(), length); } else { byte[] tmp = ByteBufUtil.threadLocalTempArray(length); ByteBuffer tmpBuf = internalNioBuffer(); tmpBuf.clear().position(index); tmpBuf.get(tmp, 0, length); out.write(tmp, 0, length); } return this; }
@Override public int setBytes(int index, InputStream in, int length) throws IOException { ensureAccessible(); if (buffer.hasArray()) { return in.read(buffer.array(), buffer.arrayOffset() + index, length); } else { byte[] tmp = ByteBufUtil.threadLocalTempArray(length); int readBytes = in.read(tmp, 0, length); if (readBytes <= 0) { return readBytes; } ByteBuffer tmpBuf = internalNioBuffer(); tmpBuf.clear().position(index); tmpBuf.put(tmp, 0, readBytes); return readBytes; } }
@Override public int setBytes(int index, InputStream in, int length) throws IOException { checkIndex(index, length); byte[] tmp = ByteBufUtil.threadLocalTempArray(length); int readBytes = in.read(tmp, 0, length); if (readBytes <= 0) { return readBytes; } ByteBuffer tmpBuf = internalNioBuffer(); tmpBuf.clear().position(idx(index)); tmpBuf.put(tmp, 0, readBytes); return readBytes; }
@SuppressWarnings("deprecation") static String decodeString(ByteBuf src, int readerIndex, int len, Charset charset) { if (len == 0) { return StringUtil.EMPTY_STRING; } final byte[] array; final int offset; if (src.hasArray()) { array = src.array(); offset = src.arrayOffset() + readerIndex; } else { array = threadLocalTempArray(len); offset = 0; src.getBytes(readerIndex, array, 0, len); } if (CharsetUtil.US_ASCII.equals(charset)) { // Fast-path for US-ASCII which is used frequently. return new String(array, 0, offset, len); } return new String(array, offset, len, charset); }
/** * Read bytes from the given {@link ByteBuffer} into the given {@link OutputStream} using the {@code position} and * {@code length}. The position and limit of the given {@link ByteBuffer} may be adjusted. */ static void readBytes(ByteBufAllocator allocator, ByteBuffer buffer, int position, int length, OutputStream out) throws IOException { if (buffer.hasArray()) { out.write(buffer.array(), position + buffer.arrayOffset(), length); } else { int chunkLen = Math.min(length, WRITE_CHUNK_SIZE); buffer.clear().position(position); if (length <= MAX_TL_ARRAY_LEN || !allocator.isDirectBufferPooled()) { getBytes(buffer, threadLocalTempArray(chunkLen), 0, chunkLen, out, length); } else { // if direct buffers are pooled chances are good that heap buffers are pooled as well. ByteBuf tmpBuf = allocator.heapBuffer(chunkLen); try { byte[] tmp = tmpBuf.array(); int offset = tmpBuf.arrayOffset(); getBytes(buffer, tmp, offset, chunkLen, out, length); } 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(); } } } }