Java Code Examples for io.netty.buffer.ByteBuf#copy()

The following examples show how to use io.netty.buffer.ByteBuf#copy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpPostStandardRequestDecoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Initialized the internals from a new chunk
 *
 * @param content
 *            the new received chunk
 * @throws ErrorDataDecoderException
 *             if there is a problem with the charset decoding or other
 *             errors
 */
@Override
public HttpPostStandardRequestDecoder offer(HttpContent content) {
    checkDestroyed();

    // Maybe we should better not copy here for performance reasons but this will need
    // more care by the caller to release the content in a correct manner later
    // So maybe something to optimize on a later stage
    ByteBuf buf = content.content();
    if (undecodedChunk == null) {
        undecodedChunk = buf.copy();
    } else {
        undecodedChunk.writeBytes(buf);
    }
    if (content instanceof LastHttpContent) {
        isLastChunk = true;
    }
    parseBody();
    if (undecodedChunk != null && undecodedChunk.writerIndex() > discardThreshold) {
        undecodedChunk.discardReadBytes();
    }
    return this;
}
 
Example 2
Source File: KeyValueHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 6 votes vote down vote up
/**
 * Since empty content is not bigger compressed, it should not send the flags.
 */
@Test
public void shouldNotCompressEmptyContent() {
    channel.pipeline().addFirst(new SnappyFeatureHandler());

    ByteBuf content = Unpooled.copiedBuffer("", CharsetUtil.UTF_8);

    UpsertRequest request = new UpsertRequest("key", content.copy(), "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);

    assertEquals(0, outbound.getDataType());
    assertEquals("", outbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.release(outbound);
}
 
Example 3
Source File: CommandDecoder.java    From redisson with Apache License 2.0 6 votes vote down vote up
private void decode(ChannelHandlerContext ctx, ByteBuf in, QueueCommand data) throws Exception {
    if (log.isTraceEnabled()) {
        log.trace("reply: {}, channel: {}, command: {}", in.toString(0, in.writerIndex(), CharsetUtil.UTF_8), ctx.channel(), data);
    }

    if (decodeInExecutor && !(data instanceof CommandsData)) {
        ByteBuf copy = in.copy(in.readerIndex(), in.writerIndex() - in.readerIndex());
        in.skipBytes(in.writerIndex() - in.readerIndex());
        executor.execute(() -> {
            state(new State());
            
            try {
                decodeCommand(ctx.channel(), copy, data);
            } catch (Exception e) {
                log.error("Unable to decode data in separate thread: " + LogHelper.toString(data), e);
            } finally {
                copy.release();
            }
        });
    } else {
        decodeCommand(ctx.channel(), in, data);
    }
}
 
Example 4
Source File: RntbdResponse.java    From azure-cosmosdb-java with MIT License 6 votes vote down vote up
/**
 * Initializes a new {@link RntbdResponse} instance.
 * <p>
 * This method is provided for testing purposes only. It should not be used in product code.
 *
 * @param activityId an activity ID
 * @param statusCode a response status code.
 * @param map a collection of response headers.
 * @param content a body to be copied to the response.
 */
public RntbdResponse(
    final UUID activityId,
    final int statusCode,
    final Map<String, String> map,
    final ByteBuf content) {

    this.headers = RntbdResponseHeaders.fromMap(map, content.readableBytes() > 0);
    this.message = Unpooled.EMPTY_BUFFER;
    this.content = content.copy();

    final HttpResponseStatus status = HttpResponseStatus.valueOf(statusCode);
    final int length = RntbdResponseStatus.LENGTH + this.headers.computeLength();

    this.frame = new RntbdResponseStatus(length, status, activityId);
    this.messageLength = length + this.content.writerIndex();
    this.referenceCount = 0;
}
 
Example 5
Source File: HttpPostStandardRequestDecoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Initialized the internals from a new chunk
 *
 * @param content
 *            the new received chunk
 * @throws ErrorDataDecoderException
 *             if there is a problem with the charset decoding or other
 *             errors
 */
@Override
public HttpPostStandardRequestDecoder offer(HttpContent content) {
    checkDestroyed();

    // Maybe we should better not copy here for performance reasons but this will need
    // more care by the caller to release the content in a correct manner later
    // So maybe something to optimize on a later stage
    ByteBuf buf = content.content();
    if (undecodedChunk == null) {
        undecodedChunk = buf.copy();
    } else {
        undecodedChunk.writeBytes(buf);
    }
    if (content instanceof LastHttpContent) {
        isLastChunk = true;
    }
    parseBody();
    if (undecodedChunk != null && undecodedChunk.writerIndex() > discardThreshold) {
        undecodedChunk.discardReadBytes();
    }
    return this;
}
 
Example 6
Source File: HttpPostMultipartRequestDecoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Initialized the internals from a new chunk
 *
 * @param content
 *            the new received chunk
 * @throws ErrorDataDecoderException
 *             if there is a problem with the charset decoding or other
 *             errors
 */
@Override
public HttpPostMultipartRequestDecoder offer(HttpContent content) {
    checkDestroyed();

    // Maybe we should better not copy here for performance reasons but this will need
    // more care by the caller to release the content in a correct manner later
    // So maybe something to optimize on a later stage
    ByteBuf buf = content.content();
    if (undecodedChunk == null) {
        undecodedChunk = buf.copy();
    } else {
        undecodedChunk.writeBytes(buf);
    }
    if (content instanceof LastHttpContent) {
        isLastChunk = true;
    }
    parseBody();
    if (undecodedChunk != null && undecodedChunk.writerIndex() > discardThreshold) {
        undecodedChunk.discardReadBytes();
    }
    return this;
}
 
Example 7
Source File: HttpResponseStreamHandlerTest.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoBytesSkipped() throws Exception {
    ResultCallbackTest callback = new ResultCallbackTest();
    HttpResponseStreamHandler streamHandler = new HttpResponseStreamHandler(callback);
    ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
    ByteBuf buffer = generateByteBuf();
    ByteBuf readBuffer = buffer.copy();
    assertEquals(buffer.refCnt(), 1);
    streamHandler.channelRead(ctx, buffer);
    streamHandler.channelInactive(ctx);
    assertEquals(buffer.refCnt(), 0);
    try (InputStream inputStream = callback.getInputStream()) {
        assertTrue(IOUtils.contentEquals(inputStream, new ByteBufInputStream(readBuffer)));
    }
    ReferenceCountUtil.release(readBuffer);
}
 
Example 8
Source File: KeyValueHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecodeReplicaGetResponse() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
        content.copy());
    response.setCAS(123456789L);
    response.setExtras(Unpooled.buffer().writeInt(123));
    response.setExtrasLength((byte) 4);

    ReplicaGetRequest requestMock = mock(ReplicaGetRequest.class);
    when(requestMock.bucket()).thenReturn(BUCKET);
    requestQueue.add(requestMock);
    channel.writeInbound(response);

    assertEquals(1, eventSink.responseEvents().size());
    GetResponse event = (GetResponse) eventSink.responseEvents().get(0).getMessage();
    assertEquals(123456789L, event.cas());
    assertEquals(123, event.flags());
    assertEquals("content", event.content().toString(CHARSET));
    assertEquals(BUCKET, event.bucket());
}
 
Example 9
Source File: KeyValueHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecodeObserveResponseDuringRebalance() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("{someconfig...}", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(new byte[] {}, Unpooled.EMPTY_BUFFER,
        content.copy());
    response.setStatus(KeyValueStatus.ERR_NOT_MY_VBUCKET.code());

    ObserveRequest requestMock = mock(ObserveRequest.class);
    requestQueue.add(requestMock);
    channel.writeInbound(response);

    assertEquals(1, eventSink.responseEvents().size());
    ObserveResponse event = (ObserveResponse) eventSink.responseEvents().get(0).getMessage();
    assertEquals(ResponseStatus.RETRY, event.status());
    assertEquals(ObserveResponse.ObserveStatus.UNKNOWN, event.observeStatus());
}
 
Example 10
Source File: KeyValueHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecodeResponseGetWithTracingData() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
        content.copy());
    response.setCAS(123456789L);
    response.setExtras(Unpooled.buffer().writeInt(123));
    response.setExtrasLength((byte) 4);
    response.setFramingExtras(Unpooled.buffer().writeByte(0x02).writeShort(1234));
    response.setFramingExtrasLength((byte) 3);

    GetRequest requestMock = mock(GetRequest.class);
    when(requestMock.bucket()).thenReturn(BUCKET);
    requestQueue.add(requestMock);
    channel.writeInbound(response);

    assertEquals(1, eventSink.responseEvents().size());
    GetResponse event = (GetResponse) eventSink.responseEvents().get(0).getMessage();
    assertEquals(123456789L, event.cas());
    assertEquals(123, event.flags());
    assertEquals("content", event.content().toString(CHARSET));
    assertEquals(119635, event.serverDuration());
    assertEquals(BUCKET, event.bucket());
    event.release();
}
 
Example 11
Source File: HttpResponseStreamHandlerTest.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadByteByByte() throws Exception {
    ResultCallbackTest callback = new ResultCallbackTest();
    HttpResponseStreamHandler streamHandler = new HttpResponseStreamHandler(callback);
    ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
    ByteBuf buffer = generateByteBuf();
    ByteBuf readBuffer = buffer.copy();
    assertEquals(buffer.refCnt(), 1);
    streamHandler.channelRead(ctx, buffer);
    streamHandler.channelInactive(ctx);
    assertEquals(buffer.refCnt(), 0);
    try (InputStream inputStream = callback.getInputStream()) {
        for (int i = 0; i < readBuffer.readableBytes(); i++) {
            int b = inputStream.read();
            assertEquals(b, readBuffer.getByte(i));
        }
        assertTrue(inputStream.read() == -1);
    }
    ReferenceCountUtil.release(readBuffer);
}
 
Example 12
Source File: NettyServer.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    LOG.trace("NettyServerHandler: Channel write: {}", msg);
    if (isWebSocketServer() && msg instanceof ByteBuf) {
        if (isFragmentWrites()) {
            ByteBuf orig = (ByteBuf) msg;
            int origIndex = orig.readerIndex();
            int split = orig.readableBytes()/2;

            ByteBuf part1 = orig.copy(origIndex, split);
            LOG.trace("NettyServerHandler: Part1: {}", part1);
            orig.readerIndex(origIndex + split);
            LOG.trace("NettyServerHandler: Part2: {}", orig);

            BinaryWebSocketFrame frame1 = new BinaryWebSocketFrame(false, 0, part1);
            ctx.writeAndFlush(frame1);
            ContinuationWebSocketFrame frame2 = new ContinuationWebSocketFrame(true, 0, orig);
            ctx.write(frame2, promise);
        } else {
            BinaryWebSocketFrame frame = new BinaryWebSocketFrame((ByteBuf) msg);
            ctx.write(frame, promise);
        }
    } else {
        ctx.write(msg, promise);
    }
}
 
Example 13
Source File: CommandsTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private int computeChecksum(MessageMetadata msgMetadata, ByteBuf compressedPayload) throws IOException {
    int metadataSize = msgMetadata.getSerializedSize();
    int metadataFrameSize = 4 + metadataSize;
    ByteBuf metaPayloadFrame = PulsarByteBufAllocator.DEFAULT.buffer(metadataFrameSize, metadataFrameSize);
    ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(metaPayloadFrame);
    metaPayloadFrame.writeInt(metadataSize);
    msgMetadata.writeTo(outStream);
    ByteBuf payload = compressedPayload.copy();
    ByteBufPair metaPayloadBuf = ByteBufPair.get(metaPayloadFrame, payload);
    int computedChecksum = Crc32cIntChecksum.computeChecksum(ByteBufPair.coalesce(metaPayloadBuf));
    outStream.recycle();
    metaPayloadBuf.release();
    return computedChecksum;
}
 
Example 14
Source File: DebugUtils.java    From cassandana with Apache License 2.0 5 votes vote down vote up
public static String payload2Str(ByteBuf content) {
    final ByteBuf copy = content.copy();
    final byte[] bytesContent;
    if (copy.isDirect()) {
        final int size = copy.readableBytes();
        bytesContent = new byte[size];
        copy.readBytes(bytesContent);
    } else {
        bytesContent = copy.array();
    }
    return new String(bytesContent, StandardCharsets.UTF_8);
}
 
Example 15
Source File: DefaultFullBinaryMemcacheRequest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Override
public FullBinaryMemcacheRequest copy() {
    ByteBuf extras = getExtras();
    if (extras != null) {
        extras = extras.copy();
    }
    return new DefaultFullBinaryMemcacheRequest(getKey(), extras, content().copy());
}
 
Example 16
Source File: DFClusterManager.java    From dfactor with MIT License 5 votes vote down vote up
protected int broadcast(String srcActor, String dstNodeType, String dstActor, int userCmd, ByteBuf userData){
	int failedNum = 0;
	DFNodeList nodeLs = null;
	if(dstNodeType == null){ //all node
		nodeLs = _getAllNodeSafe();
	}else{  //by type
		nodeLs = _getNodeByTypeSafe(dstNodeType);
	}
	if(nodeLs != null){
		DFNode curNode = null;
		Iterator<DFNode> it = nodeLs.lsNode.iterator();
		if(userData != null){  //has payload
			int nodeCount = 0;
			while(it.hasNext()){
				curNode = it.next();
				if(_sendToNodeByteBuf(srcActor, curNode.name, dstActor, null, 0, userCmd, userData, RpcParamType.BYTE_BUF, null) != 0){
					++failedNum;
				}
				if(++nodeCount < nodeLs.nodeNum) userData = userData.copy();
				else userData = null;  //last one
			}
		}else{  //no payload
			while(it.hasNext()){
				curNode = it.next();
				if(_sendToNodeByteBuf(srcActor, curNode.name, dstActor, null, 0, userCmd, null, RpcParamType.BYTE_BUF, null) != 0){
					++failedNum;
				}
			}
		}
	}
	if(userData != null){
		userData.release();
	}
	return failedNum;
}
 
Example 17
Source File: DefaultFullBinaryMemcacheResponse.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Override
public FullBinaryMemcacheResponse copy() {
    ByteBuf extras = getExtras();
    if (extras != null) {
        extras = extras.copy();
    }
    return new DefaultFullBinaryMemcacheResponse(getKey(), extras, content().copy());
}
 
Example 18
Source File: HttpPostMultipartRequestDecoder.java    From dorado with Apache License 2.0 4 votes vote down vote up
/**
 * Load the field value from a Multipart request
 *
 * @return {@code true} if the last chunk is loaded (boundary delimiter found),
 *         {@code false} if need more chunks
 * @throws ErrorDataDecoderException
 */
private static boolean loadDataMultipart(ByteBuf undecodedChunk, String delimiter, HttpData httpData) {
	if (!undecodedChunk.hasArray()) {
		return loadDataMultipartStandard(undecodedChunk, delimiter, httpData);
	}
	final SeekAheadOptimize sao = new SeekAheadOptimize(undecodedChunk);
	final int startReaderIndex = undecodedChunk.readerIndex();
	final int delimeterLength = delimiter.length();
	int index = 0;
	int lastRealPos = sao.pos;
	byte prevByte = HttpConstants.LF;
	boolean delimiterFound = false;
	while (sao.pos < sao.limit) {
		final byte nextByte = sao.bytes[sao.pos++];
		// Check the delimiter
		if (prevByte == HttpConstants.LF && nextByte == delimiter.codePointAt(index)) {
			index++;
			if (delimeterLength == index) {
				delimiterFound = true;
				break;
			}
			continue;
		}
		lastRealPos = sao.pos;
		if (nextByte == HttpConstants.LF) {
			index = 0;
			lastRealPos -= (prevByte == HttpConstants.CR) ? 2 : 1;
		}
		prevByte = nextByte;
	}
	if (prevByte == HttpConstants.CR) {
		lastRealPos--;
	}
	final int lastPosition = sao.getReadPosition(lastRealPos);
	final ByteBuf content = undecodedChunk.copy(startReaderIndex, lastPosition - startReaderIndex);
	try {
		httpData.addContent(content, delimiterFound);
	} catch (IOException e) {
		throw new ErrorDataDecoderException(e);
	}
	undecodedChunk.readerIndex(lastPosition);
	return delimiterFound;
}
 
Example 19
Source File: SocketSpdyEchoTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private static void testSpdyEcho(
        ServerBootstrap sb, Bootstrap cb, final SpdyVersion version, boolean autoRead) throws Throwable {

    ByteBuf frames;
    switch (version) {
    case SPDY_3_1:
        frames = createFrames(3);
        break;
    default:
        throw new IllegalArgumentException("unknown version");
    }

    sb.childOption(ChannelOption.AUTO_READ, autoRead);
    cb.option(ChannelOption.AUTO_READ, autoRead);

    final SpdyEchoTestServerHandler sh = new SpdyEchoTestServerHandler(autoRead);
    final SpdyEchoTestClientHandler ch = new SpdyEchoTestClientHandler(frames.copy(), autoRead);

    sb.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel channel) throws Exception {
            channel.pipeline().addLast(
                    new SpdyFrameCodec(version),
                    sh);
        }
    });

    cb.handler(ch);

    Channel sc = sb.bind().sync().channel();
    int port = ((InetSocketAddress) sc.localAddress()).getPort();

    Channel cc = cb.connect(sc.localAddress()).sync().channel();
    cc.writeAndFlush(frames);

    while (ch.counter < frames.writerIndex() - ignoredBytes) {
        if (sh.exception.get() != null) {
            break;
        }
        if (ch.exception.get() != null) {
            break;
        }

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            // Ignore.
        }
    }

    if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) {
        throw ch.exception.get();
    }
    if (sh.exception.get() != null) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null) {
        throw ch.exception.get();
    }
}
 
Example 20
Source File: KeyValueHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldEncodeReplaceRequest() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);

    ReplaceRequest request = new ReplaceRequest("key", content.copy(), "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals(0, outbound.getCAS());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);

    request = new ReplaceRequest("key", content.copy(), 0, 10, 0, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(10, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);

    request = new ReplaceRequest("key", content.copy(), 0, 0, 5, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(5, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);

    request = new ReplaceRequest("key", content.copy(), 0, 30, 99, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", new String(outbound.getKey(), CHARSET));
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(KeyValueHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(99, outbound.getExtras().readInt());
    assertEquals(30, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(outbound);

    ReferenceCountUtil.release(content);
}