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

The following examples show how to use io.netty.buffer.ByteBuf#duplicate() . 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: Http2RequestHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void handleImage(String x, String y, ChannelHandlerContext ctx, String streamId, int latency,
        FullHttpRequest request) {
    ByteBuf image = ImageCache.INSTANCE.image(parseInt(x), parseInt(y));
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, image.duplicate());
    response.headers().set(CONTENT_TYPE, "image/jpeg");
    sendResponse(ctx, streamId, latency, response, request);
}
 
Example 2
Source File: DefaultFullBinaryMemcacheRequest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public FullBinaryMemcacheRequest duplicate() {
    ByteBuf key = key();
    if (key != null) {
        key = key.duplicate();
    }
    ByteBuf extras = extras();
    if (extras != null) {
        extras = extras.duplicate();
    }
    return new DefaultFullBinaryMemcacheRequest(key, extras, content().duplicate());
}
 
Example 3
Source File: DefaultFullBinaryMemcacheResponse.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public FullBinaryMemcacheResponse duplicate() {
    ByteBuf key = key();
    if (key != null) {
        key = key.duplicate();
    }
    ByteBuf extras = extras();
    if (extras != null) {
        extras = extras.duplicate();
    }
    return new DefaultFullBinaryMemcacheResponse(key, extras, content().duplicate());
}
 
Example 4
Source File: SimpleFrameCodec.java    From drift with Apache License 2.0 5 votes vote down vote up
private int extractResponseSequenceId(ByteBuf buffer)
        throws TTransportException
{
    TChannelBufferInputTransport inputTransport = new TChannelBufferInputTransport(buffer.duplicate());
    try {
        TMessage message = protocol.createProtocol(inputTransport).readMessageBegin();
        return message.getSequenceId();
    }
    catch (Throwable e) {
        throw new TTransportException("Could not find sequenceId in Thrift message", e);
    }
    finally {
        inputTransport.release();
    }
}
 
Example 5
Source File: NetFlowV9Parser.java    From graylog-plugin-netflow with Apache License 2.0 5 votes vote down vote up
public static RawNetFlowV9Packet parsePacketShallow(ByteBuf bb) {
    final ByteBuf buf = bb.duplicate();

    final int dataLength = buf.readableBytes();
    final NetFlowV9Header header = parseHeader(buf);
    final Map<Integer, byte[]> allTemplates = Maps.newHashMap();
    Map.Entry<Integer, byte[]> optTemplate = null;
    final Set<Integer> usedTemplates = Sets.newHashSet();

    while (buf.isReadable()) {
        buf.markReaderIndex();
        int flowSetId = buf.readUnsignedShort();
        if (flowSetId == 0) {
            final List<Map.Entry<Integer, byte[]>> templates = parseTemplatesShallow(buf);
            for (Map.Entry<Integer, byte[]> t : templates) {
                allTemplates.put(t.getKey(), t.getValue());
            }
        } else if (flowSetId == 1) {
            optTemplate = parseOptionTemplateShallow(buf);
        } else {
            buf.resetReaderIndex();
            usedTemplates.add(parseRecordShallow(buf));
        }
    }

    return RawNetFlowV9Packet.create(header, dataLength, allTemplates, optTemplate, usedTemplates);
}
 
Example 6
Source File: CoreMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private RuntimeException onCheckPropertiesError(Throwable e) {
   // This is not an expected error, hence no specific logger created
   logger.warn("Could not decode properties for CoreMessage[messageID=" + messageID + ",durable=" + durable + ",userID=" + userID + ",priority=" + priority +
                  ", timestamp=" + timestamp + ",expiration=" + expiration + ",address=" + address + ", propertiesLocation=" + propertiesLocation, e);
   final ByteBuf buffer = this.buffer;
   if (buffer != null) {
      //risky: a racy modification to buffer indexes could break this duplicate operation
      final ByteBuf duplicatebuffer = buffer.duplicate();
      duplicatebuffer.readerIndex(0);
      logger.warn("Failed message has messageID=" + messageID + " and the following buffer:\n" + ByteBufUtil.prettyHexDump(duplicatebuffer));
   } else {
      logger.warn("Failed message has messageID=" + messageID + " and the buffer was null");
   }
   return new RuntimeException(e.getMessage(), e);
}
 
Example 7
Source File: DefaultFullBinaryMemcacheRequest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Override
public FullBinaryMemcacheRequest replace(ByteBuf content) {
    ByteBuf extras = getExtras();
    if (extras != null) {
        extras = extras.duplicate();
    }
    return new DefaultFullBinaryMemcacheRequest(getKey(), extras, content);
}
 
Example 8
Source File: DefaultFullBinaryMemcacheResponse.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Override
public FullBinaryMemcacheResponse replace(ByteBuf content) {
    ByteBuf extras = getExtras();
    if (extras != null) {
        extras = extras.duplicate();
    }
    return new DefaultFullBinaryMemcacheResponse(getKey(), extras, content);
}
 
Example 9
Source File: Http2FrameCodecTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelFuture writeData(ChannelHandlerContext ctx, int streamId, ByteBuf data,
                               int padding, boolean endStream, ChannelPromise promise) {
    // duplicate 'data' to prevent readerIndex from being changed, to ease verification
    return super.writeData(ctx, streamId, data.duplicate(), padding, endStream, promise);
}
 
Example 10
Source File: MqttPublishPayload.java    From mithqtt with Apache License 2.0 4 votes vote down vote up
public MqttPublishPayload(ByteBuf buf) {
    ByteBuf b = buf.duplicate();
    this.bytes = new byte[b.readableBytes()];
    b.readBytes(this.bytes);
}
 
Example 11
Source File: FixLengthDecoderTest.java    From netty-learning with MIT License 3 votes vote down vote up
@Test
public void decode() throws Exception {

    ByteBuf buf = Unpooled.buffer();

    for (int i = 0; i < 9; i++) {
        buf.writeByte(i);
    }

    ByteBuf duplicate = buf.duplicate();

    EmbeddedChannel channel = new EmbeddedChannel(new FixLengthDecoder(3));
    channel.writeInbound(buf.retain());
    channel.finish();

    ByteBuf read = channel.readInbound();
    Assert.assertEquals(duplicate.readSlice(3), read);


    read = channel.readInbound();
    Assert.assertEquals(duplicate.readSlice(3), read);

    read = channel.readInbound();
    Assert.assertEquals(duplicate.readSlice(3), read);

    read.release();


}
 
Example 12
Source File: FixLengthDecoderTest.java    From netty-learning with MIT License 3 votes vote down vote up
@Test
public void decode2() throws Exception {

    ByteBuf buf = Unpooled.buffer();

    for (int i = 0; i < 9; i++) {
        buf.writeByte(i);
    }

    ByteBuf duplicate = buf.duplicate();

    EmbeddedChannel channel = new EmbeddedChannel(new FixLengthDecoder(3));
    Assert.assertFalse(channel.writeInbound(buf.readBytes(2)));
    Assert.assertTrue(channel.writeInbound(buf.readBytes(7)));


    channel.finish();

    ByteBuf read = channel.readInbound();
    Assert.assertEquals(duplicate.readSlice(3), read);


    read = channel.readInbound();
    Assert.assertEquals(duplicate.readSlice(3), read);

    read = channel.readInbound();
    Assert.assertEquals(duplicate.readSlice(3), read);

    read.release();


}
 
Example 13
Source File: ActiveMQBuffers.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an ActiveMQBuffer wrapping an underlying ByteBuf
 *
 * The position on this buffer won't affect the position on the inner buffer
 *
 * @param underlying the underlying NIO ByteBuffer
 * @return an ActiveMQBuffer wrapping the underlying NIO ByteBuffer
 */
public static ActiveMQBuffer wrappedBuffer(final ByteBuf underlying) {
   ActiveMQBuffer buff = new ChannelBufferWrapper(underlying.duplicate());

   return buff;
}