Example usage for io.netty.buffer ByteBuf release

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

Introduction

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

Prototype

boolean release();

Source Link

Document

Decreases the reference count by 1 and deallocates this object if the reference count reaches at 0 .

Usage

From source file:com.yahoo.pulsar.common.api.PulsarDecoder.java

License:Apache License

@Override
final public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // Get a buffer that contains the full frame
    ByteBuf buffer = (ByteBuf) msg;
    BaseCommand cmd = null;/*from ww w . j  a v  a2  s .  co  m*/
    BaseCommand.Builder cmdBuilder = null;

    try {
        // De-serialize the command
        int cmdSize = (int) buffer.readUnsignedInt();
        int writerIndex = buffer.writerIndex();
        buffer.writerIndex(buffer.readerIndex() + cmdSize);
        ByteBufCodedInputStream cmdInputStream = ByteBufCodedInputStream.get(buffer);
        cmdBuilder = BaseCommand.newBuilder();
        cmd = cmdBuilder.mergeFrom(cmdInputStream, null).build();
        buffer.writerIndex(writerIndex);

        cmdInputStream.recycle();

        if (log.isDebugEnabled()) {
            log.debug("[{}] Received cmd {}", ctx.channel().remoteAddress(), cmd.getType());
        }

        messageReceived();

        switch (cmd.getType()) {
        case PARTITIONED_METADATA:
            checkArgument(cmd.hasPartitionMetadata());
            handlePartitionMetadataRequest(cmd.getPartitionMetadata());
            cmd.getPartitionMetadata().recycle();
            break;

        case PARTITIONED_METADATA_RESPONSE:
            checkArgument(cmd.hasPartitionMetadataResponse());
            handlePartitionResponse(cmd.getPartitionMetadataResponse());
            cmd.getPartitionMetadataResponse().recycle();
            break;

        case LOOKUP:
            checkArgument(cmd.hasLookupTopic());
            handleLookup(cmd.getLookupTopic());
            cmd.getLookupTopic().recycle();
            break;

        case LOOKUP_RESPONSE:
            checkArgument(cmd.hasLookupTopicResponse());
            handleLookupResponse(cmd.getLookupTopicResponse());
            cmd.getLookupTopicResponse().recycle();
            break;

        case ACK:
            checkArgument(cmd.hasAck());
            handleAck(cmd.getAck());
            cmd.getAck().getMessageId().recycle();
            cmd.getAck().recycle();
            break;

        case CLOSE_CONSUMER:
            checkArgument(cmd.hasCloseConsumer());
            handleCloseConsumer(cmd.getCloseConsumer());
            cmd.getCloseConsumer().recycle();
            break;

        case CLOSE_PRODUCER:
            checkArgument(cmd.hasCloseProducer());
            handleCloseProducer(cmd.getCloseProducer());
            cmd.getCloseProducer().recycle();
            break;

        case CONNECT:
            checkArgument(cmd.hasConnect());
            handleConnect(cmd.getConnect());
            cmd.getConnect().recycle();
            break;
        case CONNECTED:
            checkArgument(cmd.hasConnected());
            handleConnected(cmd.getConnected());
            cmd.getConnected().recycle();
            break;

        case ERROR:
            checkArgument(cmd.hasError());
            handleError(cmd.getError());
            cmd.getError().recycle();
            break;

        case FLOW:
            checkArgument(cmd.hasFlow());
            handleFlow(cmd.getFlow());
            cmd.getFlow().recycle();
            break;

        case MESSAGE: {
            checkArgument(cmd.hasMessage());
            handleMessage(cmd.getMessage(), buffer);
            cmd.getMessage().recycle();
            break;
        }
        case PRODUCER:
            checkArgument(cmd.hasProducer());
            handleProducer(cmd.getProducer());
            cmd.getProducer().recycle();
            break;

        case SEND: {
            checkArgument(cmd.hasSend());

            // Store a buffer marking the content + headers
            ByteBuf headersAndPayload = buffer.markReaderIndex();
            handleSend(cmd.getSend(), headersAndPayload);
            cmd.getSend().recycle();
            break;
        }
        case SEND_ERROR:
            checkArgument(cmd.hasSendError());
            handleSendError(cmd.getSendError());
            cmd.getSendError().recycle();
            break;

        case SEND_RECEIPT:
            checkArgument(cmd.hasSendReceipt());
            handleSendReceipt(cmd.getSendReceipt());
            cmd.getSendReceipt().recycle();
            break;

        case SUBSCRIBE:
            checkArgument(cmd.hasSubscribe());
            handleSubscribe(cmd.getSubscribe());
            cmd.getSubscribe().recycle();
            break;

        case SUCCESS:
            checkArgument(cmd.hasSuccess());
            handleSuccess(cmd.getSuccess());
            cmd.getSuccess().recycle();
            break;

        case PRODUCER_SUCCESS:
            checkArgument(cmd.hasProducerSuccess());
            handleProducerSuccess(cmd.getProducerSuccess());
            cmd.getProducerSuccess().recycle();
            break;

        case UNSUBSCRIBE:
            checkArgument(cmd.hasUnsubscribe());
            handleUnsubscribe(cmd.getUnsubscribe());
            cmd.getUnsubscribe().recycle();
            break;

        case PING:
            checkArgument(cmd.hasPing());
            handlePing(cmd.getPing());
            cmd.getPing().recycle();
            break;

        case PONG:
            checkArgument(cmd.hasPong());
            handlePong(cmd.getPong());
            cmd.getPong().recycle();
            break;

        case REDELIVER_UNACKNOWLEDGED_MESSAGES:
            checkArgument(cmd.hasRedeliverUnacknowledgedMessages());
            handleRedeliverUnacknowledged(cmd.getRedeliverUnacknowledgedMessages());
            cmd.getRedeliverUnacknowledgedMessages().recycle();
            break;
        }

    } finally {
        if (cmdBuilder != null) {
            cmdBuilder.recycle();
        }

        if (cmd != null) {
            cmd.recycle();
        }

        buffer.release();
    }
}

From source file:com.yahoo.pulsar.common.compression.CommandsTest.java

License:Apache License

private int computeChecksum(MessageMetadata msgMetadata, ByteBuf compressedPayload) throws IOException {
    int metadataSize = msgMetadata.getSerializedSize();
    int metadataFrameSize = 4 + metadataSize;
    ByteBuf metaPayloadFrame = PooledByteBufAllocator.DEFAULT.buffer(metadataFrameSize, metadataFrameSize);
    ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(metaPayloadFrame);
    metaPayloadFrame.writeInt(metadataSize);
    msgMetadata.writeTo(outStream);//from  w w  w.j  av a  2  s.  c o m
    ByteBuf payload = compressedPayload.copy();
    ByteBuf metaPayloadBuf = DoubleByteBuf.get(metaPayloadFrame, payload);
    int computedChecksum = Crc32cChecksum.computeChecksum(metaPayloadBuf);
    outStream.recycle();
    metaPayloadBuf.release();
    return computedChecksum;
}

From source file:com.yahoo.pulsar.common.compression.CompressorCodecTest.java

License:Apache License

@Test(dataProvider = "codec")
void testCompressDecompress(CompressionType type) throws IOException {
    CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(type);
    byte[] data = text.getBytes();
    ByteBuf raw = PooledByteBufAllocator.DEFAULT.buffer();
    raw.writeBytes(data);/*  w ww .  j  ava  2  s. co  m*/

    ByteBuf compressed = codec.encode(raw);
    assertEquals(raw.readableBytes(), data.length);

    int compressedSize = compressed.readableBytes();

    ByteBuf uncompressed = codec.decode(compressed, data.length);

    assertEquals(compressed.readableBytes(), compressedSize);

    assertEquals(uncompressed.readableBytes(), data.length);
    assertEquals(uncompressed, raw);

    raw.release();
    compressed.release();
    uncompressed.release();

    // Verify compression codecs have the same behavior with buffers ref counting
    assertEquals(raw.refCnt(), 0);
    assertEquals(compressed.refCnt(), 0);
    assertEquals(compressed.refCnt(), 0);
}

From source file:com.yahoo.pulsar.common.compression.CompressorCodecTest.java

License:Apache License

@Test(dataProvider = "codec")
void testMultpileUsages(CompressionType type) throws IOException {
    CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(type);
    byte[] data = text.getBytes();

    for (int i = 0; i < 5; i++) {
        ByteBuf raw = PooledByteBufAllocator.DEFAULT.buffer();
        raw.writeBytes(data);/*ww w  . j  a v  a  2s .  c o m*/
        ByteBuf compressed = codec.encode(raw);
        assertEquals(raw.readableBytes(), data.length);

        int compressedSize = compressed.readableBytes();

        ByteBuf uncompressed = codec.decode(compressed, data.length);

        assertEquals(compressed.readableBytes(), compressedSize);

        assertEquals(uncompressed.readableBytes(), data.length);
        assertEquals(uncompressed, raw);

        raw.release();
        compressed.release();
        uncompressed.release();
    }
}

From source file:com.yahoo.pulsar.common.compression.Crc32cChecksumTest.java

License:Apache License

@Test
public void testCrc32c() {
    ByteBuf payload = Unpooled.wrappedBuffer(inputBytes);
    int checksum = Crc32cChecksum.computeChecksum(payload);
    payload.release();
    assertEquals(expectedChecksum, checksum);
}

From source file:com.yahoo.pulsar.common.compression.Crc32cChecksumTest.java

License:Apache License

@Test
public void testCrc32cDirectMemoryHardware() {
    if (HARDWARE_CRC32C_HASH == null) {
        return;/*from   w  w  w .j av a 2 s  . c o m*/
    }

    ByteBuf payload = ByteBufAllocator.DEFAULT.directBuffer(inputBytes.length);
    payload.writeBytes(inputBytes);

    // read directly from memory address
    int checksum = HARDWARE_CRC32C_HASH.calculate(payload.memoryAddress(), payload.readableBytes());

    payload.release();
    assertEquals(checksum, expectedChecksum);
}

From source file:com.yahoo.pulsar.common.compression.Crc32cChecksumTest.java

License:Apache License

@Test
public void testCrc32cIncrementalUsingProvider() {

    final byte[] data = "data".getBytes();
    final byte[] doubleData = "datadata".getBytes();
    ByteBuf payload = Unpooled.wrappedBuffer(data);
    ByteBuf doublePayload = Unpooled.wrappedBuffer(doubleData);

    int expectedChecksum = Crc32cChecksum.computeChecksum(doublePayload);

    // (1) heap-memory
    int checksum = Crc32cChecksum.computeChecksum(payload);
    int incrementalChecksum = Crc32cChecksum.resumeChecksum(checksum, payload);
    assertEquals(expectedChecksum, incrementalChecksum);
    payload.release();
    doublePayload.release();/*from  ww w . j a va 2s  . co  m*/

    // (2) direct-memory
    payload = ByteBufAllocator.DEFAULT.directBuffer(data.length);
    payload.writeBytes(data);
    checksum = Crc32cChecksum.computeChecksum(payload);
    incrementalChecksum = Crc32cChecksum.resumeChecksum(checksum, payload);
    assertEquals(expectedChecksum, incrementalChecksum);
    payload.release();

}

From source file:com.yea.remote.netty.codec.NettyMessageDecoder.java

License:Apache License

@Override
public Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }// w w  w . j  a v  a 2  s  .  c  om
    ISerializer serializer = serializePool.borrow();
    try {
        Message message = new Message();
        byte[] sessionID = new byte[16];
        Header header = new Header();
        header.setCrcCode(frame.readInt());
        header.setLength(frame.readInt());
        frame.readBytes(sessionID);
        header.setSessionID(sessionID);
        header.setType(frame.readByte());
        header.setPriority(frame.readByte());
        header.setResult(frame.readByte());
        RemoteConstants.CompressionAlgorithm compressionAlgorithm = RemoteConstants.CompressionAlgorithm
                .valueOf(frame.readByte());
        if (compressionAlgorithm != null && compressionAlgorithm.code() > 0) {
            //
            serializer.setCompress(new Compress().setCompressionAlgorithm(compressionAlgorithm.algorithm()));
        }
        long basedate = frame.readLong();

        int attachmentSize = frame.readByte();
        if (attachmentSize > 0) {
            attachmentSize = frame.readByte();//?Date?String?
            if (attachmentSize > 0) {
                byte[] keyArray = null;
                byte[] valueArray = null;
                for (int i = 0; i < attachmentSize; i++) {
                    keyArray = new byte[frame.readShort()];
                    frame.readBytes(keyArray);
                    valueArray = new byte[frame.readShort()];
                    frame.readBytes(valueArray);

                    header.addAttachment(new String(keyArray, "ISO-8859-1"),
                            serializer.deserialize(valueArray));
                }
            }
            attachmentSize = frame.readByte();//Date?
            if (attachmentSize > 0) {
                byte[] keyArray = null;
                byte length = 0;
                for (int i = 0; i < attachmentSize; i++) {
                    keyArray = new byte[frame.readShort()];
                    frame.readBytes(keyArray);
                    length = frame.readByte();
                    if (length == 1) {
                        header.addAttachment(new String(keyArray, "ISO-8859-1"),
                                new Date(basedate - frame.readByte()));
                    } else if (length == 2) {
                        header.addAttachment(new String(keyArray, "ISO-8859-1"),
                                new Date(basedate - frame.readShort()));
                    } else if (length == 4) {
                        header.addAttachment(new String(keyArray, "ISO-8859-1"),
                                new Date(basedate - frame.readInt()));
                    } else {
                        header.addAttachment(new String(keyArray, "ISO-8859-1"),
                                new Date(basedate - frame.readLong()));
                    }
                }
            }
            attachmentSize = frame.readByte();//String?
            if (attachmentSize > 0) {
                byte[] keyArray = null;
                byte[] valueArray = null;
                for (int i = 0; i < attachmentSize; i++) {
                    keyArray = new byte[frame.readShort()];
                    frame.readBytes(keyArray);
                    valueArray = new byte[frame.readShort()];
                    frame.readBytes(valueArray);

                    header.addAttachment(new String(keyArray, "ISO-8859-1"),
                            new String(valueArray, "ISO-8859-1"));
                }
            }
        }
        header.addAttachment(NettyConstants.MessageHeaderAttachment.SEND_DATE.value(), new Date(basedate));
        header.addAttachment(NettyConstants.MessageHeaderAttachment.RECIEVE_DATE.value(), new Date());
        message.setHeader(header);

        if (frame.readableBytes() > 4) {
            int length = frame.readInt();//Body
            byte[] objArray = new byte[length];
            frame.readBytes(objArray);
            try {
                Object body = serializer.deserialize(objArray);
                message.setBody(body);
            } catch (Exception ex) {
                if (RemoteConstants.MessageType.SERVICE_RESP.value() == message.getHeader().getType()) {
                    message.getHeader().setType(RemoteConstants.MessageType.SUSPEND_RESP.value());
                    message.getHeader().setResult(RemoteConstants.MessageResult.FAILURE.value());
                    message.setBody(ex);
                } else if (RemoteConstants.MessageType.SERVICE_REQ.value() == message.getHeader().getType()) {
                    message.getHeader().setType(RemoteConstants.MessageType.SUSPEND_REQ.value());
                    message.getHeader().setResult(RemoteConstants.MessageResult.FAILURE.value());
                    message.setBody(ex);
                } else {
                    throw ex;
                }
            }
        }

        return message;
    } finally {
        serializePool.restore(serializer);
        frame.release();
    }
}

From source file:com.yeetor.androidcontrol.WSSocketHandler.java

License:Open Source License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req,
        DefaultFullHttpResponse res) {/*www. ja  v a2  s .c om*/
    // 
    if (res.getStatus().code() != 200) {
        ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);
        buf.release();
    }
    // ?Keep-Alive
    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!isKeepAlive(req) || res.getStatus().code() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.zank.websocket.server.ServerHandler.java

License:Apache License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    if (res.status().code() != 200) {
        ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);/*from ww w.j a  v a2s .c  o m*/
        buf.release();
        HttpHeaderUtil.setContentLength(res, res.content().readableBytes());
    }

    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!HttpHeaderUtil.isKeepAlive(req) || res.status().code() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}