/** * Create a copy of the underlying storage from {@code buf} into a byte array. * The copy will start at {@code start} and copy {@code length} bytes. */ public static byte[] getBytes(ByteBuf buf, int start, int length) { return getBytes(buf, start, length, true); }
/** * Create a copy of the underlying storage from {@code buf} into a byte array. * The copy will start at {@code start} and copy {@code length} bytes. */ public static byte[] getBytes(ByteBuf buf, int start, int length) { return getBytes(buf, start, length, true); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { // copy the ByteBuf content to a byte array out.add(ByteBufUtil.getBytes(msg)); } }
@Override public byte[] encode(ByteBuf message) { if (message == null) { return null; } return ByteBufUtil.getBytes(message); }
/** * Create a copy of the underlying storage from {@code buf} into a byte array. * The copy will start at {@link ByteBuf#readerIndex()} and copy {@link ByteBuf#readableBytes()} bytes. */ public static byte[] getBytes(ByteBuf buf) { return getBytes(buf, buf.readerIndex(), buf.readableBytes()); }
/** * Create a copy of the underlying storage from {@code buf} into a byte array. * The copy will start at {@code start} and copy {@code length} bytes. */ public static byte[] getBytes(ByteBuf buf, int start, int length) { return getBytes(buf, start, length, true); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { // copy the ByteBuf content to a byte array out.add(ByteBufUtil.getBytes(msg)); } }
/** * Create a copy of the underlying storage from {@code buf} into a byte array. * The copy will start at {@link ByteBuf#readerIndex()} and copy {@link ByteBuf#readableBytes()} bytes. */ public static byte[] getBytes(ByteBuf buf) { return getBytes(buf, buf.readerIndex(), buf.readableBytes()); }
@Override public CharSequence getCharSequence(int index, int length, Charset charset) { if (CharsetUtil.US_ASCII.equals(charset) || CharsetUtil.ISO_8859_1.equals(charset)) { // ByteBufUtil.getBytes(...) will return a new copy which the AsciiString uses directly return new AsciiString(ByteBufUtil.getBytes(this, index, length, true), false); } return toString(index, length, charset); }
@Override public CharSequence getCharSequence(int index, int length, Charset charset) { if (CharsetUtil.US_ASCII.equals(charset) || CharsetUtil.ISO_8859_1.equals(charset)) { // ByteBufUtil.getBytes(...) will return a new copy which the AsciiString uses directly return new AsciiString(ByteBufUtil.getBytes(this, index, length, true), false); } return toString(index, length, charset); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { final byte[] array; final int offset; final int length = msg.readableBytes(); if (msg.hasArray()) { array = msg.array(); offset = msg.arrayOffset() + msg.readerIndex(); } else { array = ByteBufUtil.getBytes(msg, msg.readerIndex(), length, false); offset = 0; } MessageNano prototype = clazz.getConstructor().newInstance(); out.add(MessageNano.mergeFrom(prototype, array, offset, length)); } }
/** * @return A converter that converts composite bytebufs to byte arrays */ @Singleton @Bean TypeConverter<CompositeByteBuf, byte[]> compositeByteBufTypeConverter() { return (object, targetType, context) -> Optional.of(ByteBufUtil.getBytes(object)); }
/** * 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(); } } } }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { final byte[] array; final int offset; final int length = msg.readableBytes(); if (msg.hasArray()) { array = msg.array(); offset = msg.arrayOffset() + msg.readerIndex(); } else { array = ByteBufUtil.getBytes(msg, msg.readerIndex(), length, false); offset = 0; } MessageNano prototype = clazz.getConstructor().newInstance(); out.add(MessageNano.mergeFrom(prototype, array, offset, length)); } }
private void cancelGoAwayStreams(int lastStreamId, long errorCode, ByteBuf debugData) { Iterator<PendingStream> iter = pendingStreams.values().iterator(); Exception e = new Http2GoAwayException(lastStreamId, errorCode, ByteBufUtil.getBytes(debugData)); while (iter.hasNext()) { PendingStream stream = iter.next(); if (stream.streamId > lastStreamId) { iter.remove(); stream.close(e); } } }
@Override protected void onData(ByteBufHolder message) { ByteBuf content = message.content(); try { byte[] bytes = ByteBufUtil.getBytes(content); jacksonProcessor.onNext(bytes); } finally { ReferenceCountUtil.release(content); } }
offset = msg.arrayOffset() + msg.readerIndex(); } else { array = ByteBufUtil.getBytes(msg, msg.readerIndex(), length, false); offset = 0;
public static byte[] uncompressedFrame(ByteBuf proto) { final ByteBuf buf = Unpooled.buffer(); buf.writeByte(0); buf.writeInt(proto.readableBytes()); buf.writeBytes(proto); proto.release(); final byte[] result = ByteBufUtil.getBytes(buf); buf.release(); return result; }
/** * Returns a memory-based {@link DataBuffer} which will be garbage-collected. */ private DataBuffer withDataBufferFactory(ByteBufHttpData data) { final byte[] dataArray = ByteBufUtil.getBytes(data.content()); data.release(); return delegate.wrap(dataArray); } }
@Test public void serializeResponse() throws Exception { final ByteBuf serialized = marshaller.serializeResponse(GrpcTestUtil.RESPONSE_MESSAGE); assertThat(ByteBufUtil.getBytes(serialized)) .containsExactly(GrpcTestUtil.RESPONSE_MESSAGE.toByteArray()); serialized.release(); }