Example usage for io.netty.buffer ByteBuf retain

List of usage examples for io.netty.buffer ByteBuf retain

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf retain.

Prototype

@Override
    public abstract ByteBuf retain();

Source Link

Usage

From source file:io.airlift.drift.transport.netty.codec.SimpleFrameCodec.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext context, Object message) throws Exception {
    if (message instanceof ByteBuf) {
        ByteBuf buffer = (ByteBuf) message;
        if (buffer.isReadable()) {
            context.fireChannelRead(new ThriftFrame(extractResponseSequenceId(buffer.retain()), buffer,
                    ImmutableMap.of(), transport, protocol, assumeClientsSupportOutOfOrderResponses));
            return;
        }/*from   w w  w.  j  a  v  a2  s.c o  m*/
    }
    context.fireChannelRead(message);
}

From source file:io.airlift.drift.transport.netty.codec.ThriftUnframedDecoder.java

License:Apache License

@Override
protected final void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) {
    int frameOffset = buffer.readerIndex();
    TChannelBufferInputTransport transport = new TChannelBufferInputTransport(buffer.retain());
    try {// w  ww.j  a  va  2s .co  m
        TProtocolReader protocolReader = protocol.createProtocol(transport);

        TMessage message = protocolReader.readMessageBegin();
        TProtocolUtil.skip(protocolReader, TType.STRUCT);
        protocolReader.readMessageEnd();

        int frameLength = buffer.readerIndex() - frameOffset;
        if (frameLength > maxFrameSize) {
            FrameInfo frameInfo = new FrameInfo(message.getName(), message.getType(), message.getSequenceId(),
                    UNFRAMED, protocol, assumeClientsSupportOutOfOrderResponses);
            ctx.fireExceptionCaught(
                    new FrameTooLargeException(Optional.of(frameInfo), frameLength, maxFrameSize));
        }

        out.add(buffer.slice(frameOffset, frameLength).retain());
    } catch (Throwable th) {
        buffer.readerIndex(frameOffset);
    } finally {
        transport.release();
    }
}

From source file:io.aos.netty5.http2.server.HelloWorldHttp2Handler.java

License:Apache License

/**
 * If receive a frame with end-of-stream set, send a pre-canned response.
 *///from   ww  w  . j  a  va2 s .  c  o m
@Override
public void onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream)
        throws Http2Exception {
    if (endOfStream) {
        sendResponse(ctx(), streamId, data.retain());
    }
}

From source file:io.gatling.http.client.test.listener.CompleteResponseListener.java

License:Apache License

@Override
public void onHttpResponseBodyChunk(ByteBuf chunk, boolean last) {

    if (chunk.isReadable()) {
        if (chunks == null) {
            chunks = new ArrayList<>(1);
        }//  ww  w  . j  a  v  a  2  s  .  c o m
        chunks.add(chunk.retain());
    }

    if (last) {
        onComplete();
    }
}

From source file:io.gatling.netty.util.ahc.ByteBufUtils.java

License:Apache License

private static ByteBuf composite(ByteBuf[] bufs) {
    for (ByteBuf buf : bufs) {
        buf.retain();
    }/*from ww w .  jav a  2  s .c  o m*/
    return Unpooled.wrappedBuffer(bufs);
}

From source file:io.grpc.alts.internal.TsiFrameHandler.java

License:Apache License

@Override
@SuppressWarnings("FutureReturnValueIgnored") // for aggregatePromise.doneAllocatingPromises
public void flush(final ChannelHandlerContext ctx) throws GeneralSecurityException {
    if (pendingUnprotectedWrites == null || pendingUnprotectedWrites.isEmpty()) {
        // Return early if there's nothing to write. Otherwise protector.protectFlush() below may
        // not check for "no-data" and go on writing the 0-byte "data" to the socket with the
        // protection framing.
        return;//  w w  w  .  j a  v  a 2s .  c  o m
    }
    // Flushes can happen after close, but only when there are no pending writes.
    checkState(protector != null, "flush() called after close()");
    final ProtectedPromise aggregatePromise = new ProtectedPromise(ctx.channel(), ctx.executor(),
            pendingUnprotectedWrites.size());
    List<ByteBuf> bufs = new ArrayList<>(pendingUnprotectedWrites.size());

    // Drain the unprotected writes.
    while (!pendingUnprotectedWrites.isEmpty()) {
        ByteBuf in = (ByteBuf) pendingUnprotectedWrites.current();
        bufs.add(in.retain());
        // Remove and release the buffer and add its promise to the aggregate.
        aggregatePromise.addUnprotectedPromise(pendingUnprotectedWrites.remove());
    }

    final class ProtectedFrameWriteFlusher implements Consumer<ByteBuf> {

        @Override
        public void accept(ByteBuf byteBuf) {
            ctx.writeAndFlush(byteBuf, aggregatePromise.newPromise());
        }
    }

    protector.protectFlush(bufs, new ProtectedFrameWriteFlusher(), ctx.alloc());
    // We're done writing, start the flow of promise events.
    aggregatePromise.doneAllocatingPromises();
}

From source file:io.grpc.netty.NettyHandlerTestBase.java

License:Apache License

protected final ByteBuf dataFrame(int streamId, boolean endStream, ByteBuf content) {
    // Need to retain the content since the frameWriter releases it.
    content.retain();

    ChannelHandlerContext ctx = newMockContext();
    new DefaultHttp2FrameWriter().writeData(ctx, streamId, content, 0, endStream, newPromise());
    return captureWrite(ctx);
}

From source file:io.haze.transport.tcp.TCPDecoder.java

License:Apache License

/**
 * Decode a {@link ByteBuf} into a {@link MessageContext}.
 *
 * @param context  The channel handler context.
 * @param message  The message./*from   w ww .  ja v a 2s  .c  o m*/
 * @param messages The output messages.
 *
 * @throws Exception If an error has occurred.
 */
@Override
public void decode(ChannelHandlerContext context, ByteBuf message, List<Object> messages) throws Exception {
    messages.add(
            new MessageContext(server.getTransportClient(((SocketChannel) context.channel()).remoteAddress())
                    .setAccessTime(System.currentTimeMillis()), new BufferMessage(message.retain())));
}

From source file:io.haze.transport.tcp.websocket.WebSocketFrameEncoder.java

License:Apache License

/**
 * Encode a {@link ByteBuf} into a {@link io.netty.handler.codec.http.websocketx.WebSocketFrame}.
 *
 * @param context  The channel handler context.
 * @param message  The input message./*ww  w.java2 s  . c o  m*/
 * @param messages The output messages.
 */
@Override
public void encode(ChannelHandlerContext context, ByteBuf message, List<Object> messages) throws Exception {
    messages.add(new TextWebSocketFrame(message.retain()));
}

From source file:io.lettuce.core.protocol.CommandHandlerBenchmark.java

License:Apache License

private void doBenchmark(List<Command> commandStack, ByteBuf response) throws Exception {

    commandHandler.write(CHANNEL_HANDLER_CONTEXT, commandStack, PROMISE);

    int index = response.readerIndex();
    response.retain();

    commandHandler.channelRead(CHANNEL_HANDLER_CONTEXT, response);

    // cleanup/*from   ww  w  . ja v  a2 s .  com*/
    response.readerIndex(index);
}