Example usage for io.netty.channel ChannelHandlerContext alloc

List of usage examples for io.netty.channel ChannelHandlerContext alloc

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext alloc.

Prototype

ByteBufAllocator alloc();

Source Link

Document

Return the assigned ByteBufAllocator which will be used to allocate ByteBuf s.

Usage

From source file:com.ebay.jetstream.messaging.transport.netty.compression.MessageDecompressionHandler.java

License:MIT License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {

    ByteBuf frame = (ByteBuf) super.decode(ctx, in);

    if (frame == null) {
        return null;
    }/* www. ja v  a  2  s  . c  om*/

    byte[] uncompressedbuf;

    if (m_allocBuf)
        uncompressedbuf = new byte[m_tmpBufSz];
    else
        uncompressedbuf = m_tmpBuf;

    int framelen = frame.readableBytes();

    int len = 0;

    try {

        len = Snappy.rawUncompress(frame.readBytes(framelen).array(), 0, framelen, uncompressedbuf, 0);

    } catch (Throwable t) {

        LOGGER.debug("Failed to uncompress - " + t.getLocalizedMessage());

        frame.release();
        return null;
    }

    frame.release();

    ByteBuf buf = ctx.alloc().directBuffer(len);

    return buf.writeBytes(uncompressedbuf, 0, len);

}

From source file:com.example.time.server.TimeServerHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) {
    final OffsetDateTime now = OffsetDateTime.now(zoneId);
    final long epochSecond = now.toEpochSecond();

    log.info("channel active: {}", now);

    final ByteBuf byteBuf = ctx.alloc().buffer(8);
    byteBuf.writeLong(epochSecond);//from w ww  .j  ava  2s . c o  m
    Futures.toMono(ctx.writeAndFlush(byteBuf)).subscribe(v -> ctx.close());
}

From source file:com.example.time1.client.FragmentedTimeClientHandler.java

License:Apache License

@Override
public void handlerAdded(final ChannelHandlerContext ctx) {
    this.byteBuf = ctx.alloc().buffer(4);
}

From source file:com.facebook.nifty.core.NiftyDispatcher.java

License:Apache License

private ByteBuf addFraming(ChannelHandlerContext ctx, ByteBuf response, ThriftTransportType transportType) {
    if (!response.isReadable()) {
        // Empty response from one-way message, don't frame it
        return response;
    }/*from   www.  j ava2  s  .c om*/

    if (transportType == ThriftTransportType.UNFRAMED) {
        return response;
    } else if (transportType == ThriftTransportType.FRAMED) {
        ByteBuf frameSizeBuffer = ctx.alloc().buffer(4);
        frameSizeBuffer.writeInt(response.readableBytes());
        return Unpooled.wrappedBuffer(frameSizeBuffer, response);
    } else {
        throw new UnsupportedOperationException("Header protocol is not supported");
    }
}

From source file:com.fjn.helper.frameworkex.netty.v4.discardserver.DiscardServerHandler.java

License:Apache License

/**
 * ???/* w  ww.j  a v a2 s .  c  om*/
 * @param ctx
 * @param msg
 * @throws Exception
 */
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;
    try {
        while (in.isReadable()) {
            char c = (char) in.readByte();
            content += c;
            if (c == '\n' || c == '\r') {
                if (!line.isEmpty()) {
                    System.out.println(line);
                }
                if ("quit".equals(line)) {
                    ByteBuf bf = ctx.alloc().buffer(content.getBytes().length);
                    bf.setBytes(0, content.getBytes());
                    final ChannelFuture f = ctx.writeAndFlush(bf); // (3)
                    f.addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws InterruptedException {
                            assert f == future;
                            ctx.close();
                        }
                    }); // (4)
                }
                line = "";
                continue;
            } else {
                line += c;
            }
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:com.fjn.helper.frameworkex.netty.v4.timetest.TimeClientHandler.java

License:Apache License

@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    buf = ctx.alloc().buffer(4); // (1)
}

From source file:com.fjn.helper.frameworkex.netty.v4.timetest.TimeEncoder.java

License:Apache License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    UnixTime m = (UnixTime) msg;//w ww . j  ava  2 s.c om
    ByteBuf encoded = ctx.alloc().buffer(4);
    encoded.writeInt((int) m.value());
    ctx.write(encoded, promise); // (1)
}

From source file:com.flowpowered.network.pipeline.MessageEncoder.java

License:MIT License

@Override
protected void encode(ChannelHandlerContext ctx, Message message, List<Object> out) throws Exception {
    final Protocol protocol = messageHandler.getSession().getProtocol();
    final Class<? extends Message> clazz = message.getClass();
    CodecRegistration reg = protocol.getCodecRegistration(message.getClass());
    if (reg == null) {
        throw new Exception("Unknown message type: " + clazz + ".");
    }/*www .j a  va  2 s .co m*/
    ByteBuf messageBuf = ctx.alloc().buffer();
    messageBuf = reg.getCodec().encode(messageBuf, message);

    ByteBuf headerBuf = ctx.alloc().buffer();
    headerBuf = protocol.writeHeader(headerBuf, reg, messageBuf);
    out.add(Unpooled.wrappedBuffer(headerBuf, messageBuf));
}

From source file:com.flowpowered.network.pipeline.MessageProcessorDecoder.java

License:MIT License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> frames) throws Exception {
    MessageProcessor processor = getProcessor();
    if (processor == null) {
        frames.add(buf.readBytes(buf.readableBytes()));
        return;/* w  w w .j ava 2s  .co m*/
    }
    // Eventually, we will run out of bytes and a ReplayableError will be called
    ByteBuf liveBuffer = ctx.alloc().buffer();
    liveBuffer = processor.processInbound(ctx, buf, liveBuffer);
    frames.add(liveBuffer);
}

From source file:com.flowpowered.network.pipeline.MessageProcessorEncoder.java

License:MIT License

@Override
protected void encode(ChannelHandlerContext ctx, final ByteBuf buf, List<Object> out) throws Exception {
    final MessageProcessor processor = getProcessor();
    if (processor == null) {
        out.add(buf.readBytes(buf.readableBytes()));
        return;// www.j a v a 2 s . c o m
    }
    ByteBuf toAdd = ctx.alloc().buffer();
    toAdd = processor.processOutbound(ctx, buf, toAdd);
    out.add(toAdd);
}