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:org.neo4j.bolt.v1.transport.ChunkedInput.java

License:Open Source License

public void append(ByteBuf chunk) {
    if (chunk.readableBytes() > 0) {
        chunks.add(chunk.retain());
        remaining += chunk.readableBytes();
    }//  w w  w  .  ja  va 2 s  . co  m
}

From source file:org.neo4j.ndp.transport.socket.ChunkedInput.java

License:Open Source License

public void addChunk(ByteBuf chunk) {
    if (chunk.readableBytes() > 0) {
        chunks.add(chunk.retain());
        remaining += chunk.readableBytes();
    }//from  w  w  w  .j  a v  a 2 s. c o m
}

From source file:org.opendaylight.openflowjava.protocol.impl.clients.SimpleClientFramer.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
    if (bb.readableBytes() < LENGTH_OF_HEADER) {
        LOGGER.debug("skipping bb - too few data for header: " + bb.readableBytes());
        return;/* w ww.  j ava 2 s .c  o m*/
    }

    int length = bb.getUnsignedShort(LENGTH_INDEX_IN_HEADER);
    if (bb.readableBytes() < length) {
        LOGGER.debug("skipping bb - too few data for msg: " + bb.readableBytes() + " < " + length);
        return;
    }
    LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(1));

    ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
    list.add(messageBuffer);
    messageBuffer.retain();
    bb.skipBytes(length);
}

From source file:org.opendaylight.openflowjava.protocol.impl.clients.UdpSimpleClientFramer.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext chc, DatagramPacket msg, List<Object> list) throws Exception {
    ByteBuf bb = msg.content();/*  w  w  w. j a  va2s  .c  o m*/
    if (bb.readableBytes() < LENGTH_OF_HEADER) {
        LOG.debug("skipping bb - too few data for header: {}", bb.readableBytes());
        return;
    }

    int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
    if (bb.readableBytes() < length) {
        LOG.debug("skipping bb - too few data for msg: {} < {}", bb.readableBytes(), length);
        return;
    }
    LOG.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));

    ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
    list.add(messageBuffer);
    messageBuffer.retain();
    bb.skipBytes(length);
}

From source file:org.opendaylight.openflowjava.protocol.impl.core.OFDatagramPacketHandler.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
    LOG.debug("OFDatagramPacketFramer");
    MessageConsumer consumer = UdpConnectionMap.getMessageConsumer(msg.sender());
    if (consumer == null) {
        ConnectionFacade connectionFacade = adapterFactory.createConnectionFacade(ctx.channel(), msg.sender(),
                false);/* ww  w  . j ava  2s  . c o  m*/
        connectionHandler.onSwitchConnected(connectionFacade);
        connectionFacade.checkListeners();
        UdpConnectionMap.addConnection(msg.sender(), connectionFacade);
    }
    ByteBuf bb = msg.content();
    int readableBytes = bb.readableBytes();
    if (readableBytes < LENGTH_OF_HEADER) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("skipping bytebuf - too few bytes for header: {} < {}", readableBytes, LENGTH_OF_HEADER);
            LOG.debug("bb: {}", ByteBufUtils.byteBufToHexString(bb));
        }
        return;
    }

    int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
    LOG.debug("length of actual message: {}", length);

    if (readableBytes < length) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("skipping bytebuf - too few bytes for msg: {} < {}", readableBytes, length);
            LOG.debug("bytebuffer: {}", ByteBufUtils.byteBufToHexString(bb));
        }
        return;
    }
    LOG.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));

    byte version = bb.readByte();
    if ((version == EncodeConstants.OF13_VERSION_ID) || (version == EncodeConstants.OF10_VERSION_ID)) {
        LOG.debug("detected version: {}", version);
        ByteBuf messageBuffer = bb.slice();
        out.add(new VersionMessageUdpWrapper(version, messageBuffer, msg.sender()));
        messageBuffer.retain();
    } else {
        LOG.warn("detected version: {} - currently not supported", version);
    }
    bb.skipBytes(bb.readableBytes());
}

From source file:org.opendaylight.openflowjava.protocol.impl.core.OFEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, OfHeader msg, ByteBuf out) throws Exception {
    LOGGER.trace("Encoding");
    try {/*  w  ww.  j  av a  2  s  . c o m*/
        SerializationFactory.messageToBuffer(msg.getVersion(), out, msg);
    } catch (Exception e) {
        LOGGER.error("Message serialization failed");
        LOGGER.error(e.getMessage(), e);
        out.clear();
        return;
    }
    if (out.readableBytes() > 0) {
        out.retain();
        ctx.writeAndFlush(out);
    } else {
        LOGGER.warn("Translated buffer is empty");
    }
}

From source file:org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
    int readableBytes = bb.readableBytes();
    if (readableBytes < LENGTH_OF_HEADER) {
        LOGGER.debug("skipping bb - too few data for header: " + readableBytes);
        return;/*  www .  j  av a 2  s .  co  m*/
    }

    int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
    LOGGER.debug("length of actual message: {}", length);

    if (readableBytes < length) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("skipping bb - too few data for msg: " + readableBytes + " < " + length);
            LOGGER.debug("bb: " + ByteBufUtils.byteBufToHexString(bb));
        }
        return;
    }
    LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));

    ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
    list.add(messageBuffer);
    messageBuffer.retain();
    bb.skipBytes(length);
}

From source file:org.opendaylight.openflowjava.protocol.impl.core.OFVersionDetector.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
    if (bb.readableBytes() == 0) {
        LOGGER.debug("not enough data");
        bb.release();/*from  w  ww  . j av a  2 s .co m*/
        return;
    }
    byte version = bb.readByte();
    if ((version == OF13_VERSION_ID) || (version == OF10_VERSION_ID)) {
        LOGGER.debug("detected version: " + version);
    } else {
        LOGGER.warn("detected version: " + version + " - currently not supported");
        bb.skipBytes(bb.readableBytes());
        return;
    }

    ByteBuf messageBuffer = bb.slice();
    list.add(new VersionMessageWrapper(version, messageBuffer));
    messageBuffer.retain();
    bb.skipBytes(bb.readableBytes());
}

From source file:org.proton.plug.test.invm.ProtonINVMSPI.java

License:Apache License

@Override
public void onTransport(final ByteBuf bytes, final AMQPConnectionContext connection) {
    if (DebugInfo.debug) {
        ByteUtil.debugFrame("InVM->", bytes);
    }//from ww w  .ja v  a 2 s.  c  om
    final int size = bytes.writerIndex();

    bytes.retain();
    mainExecutor.execute(new Runnable() {
        public void run() {
            try {
                if (DebugInfo.debug) {
                    ByteUtil.debugFrame("InVMDone->", bytes);
                }
                serverConnection.inputBuffer(bytes);
                try {
                    connection.outputDone(size);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } finally {
                bytes.release();
            }
        }
    });
}

From source file:org.quartzpowered.network.pipeline.CompressionHandler.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    Buffer prefixBuf = new Buffer(ctx.alloc().buffer(5));
    ByteBuf contentsBuf;/*ww  w  .  j  a v  a 2s.c  om*/

    if (msg.readableBytes() >= threshold) {
        // message should be compressed
        int index = msg.readerIndex();
        int length = msg.readableBytes();

        byte[] sourceData = new byte[length];
        msg.readBytes(sourceData);
        deflater.setInput(sourceData);
        deflater.finish();

        byte[] compressedData = new byte[length];
        int compressedLength = deflater.deflate(compressedData);
        deflater.reset();

        if (compressedLength == 0) {
            // compression failed in some weird way
            throw new EncoderException("Failed to compress message of size " + length);
        } else if (compressedLength >= length) {
            // compression increased the size. threshold is probably too low
            // send as an uncompressed packet
            prefixBuf.writeVarInt(0);
            msg.readerIndex(index);
            msg.retain();
            contentsBuf = msg;
        } else {
            // all is well
            prefixBuf.writeVarInt(length);
            contentsBuf = Unpooled.wrappedBuffer(compressedData, 0, compressedLength);
        }
    } else {
        // message should be sent through
        prefixBuf.writeVarInt(0);
        msg.retain();
        contentsBuf = msg;
    }

    out.add(Unpooled.wrappedBuffer(prefixBuf, contentsBuf));
}