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.datastax.driver.core.SnappyCompressor.java

License:Apache License

private ByteBuf decompressHeap(ByteBuf input) throws IOException {
    // Not a direct buffer so use byte arrays...
    int inOffset = input.arrayOffset() + input.readerIndex();
    byte[] in = input.array();
    int len = input.readableBytes();
    // Increase reader index.
    input.readerIndex(input.writerIndex());

    if (!Snappy.isValidCompressedBuffer(in, inOffset, len))
        throw new DriverInternalError("Provided frame does not appear to be Snappy compressed");

    // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so
    // can eliminate the overhead of allocate a new byte[].
    ByteBuf output = input.alloc().heapBuffer(Snappy.uncompressedLength(in, inOffset, len));
    try {//from   w  w  w . j a va  2 s  .  co m
        // Calculate the correct offset.
        int offset = output.arrayOffset() + output.writerIndex();
        byte[] out = output.array();
        int written = Snappy.uncompress(in, inOffset, len, out, offset);

        // Increase the writerIndex with the written bytes.
        output.writerIndex(output.writerIndex() + written);
    } catch (IOException e) {
        // release output buffer so we not leak and rethrow exception.
        output.release();
        throw e;
    }
    return output;
}

From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java

License:Apache License

/**
 * fastjson??javaBean     </br>/*w  w  w .  j  ava 2 s . c om*/
 * ??javaBean           </br>
 *
 * @param buffer buffer
 * @param clazz  class  sub-class
 * @return Object  javaBean
 */
public static Object parseObject(ByteBuf buffer, Class clazz) {
    Object o = JSON.parseObject(buffer.toString(buffer.readerIndex(),
            buffer.writerIndex() - buffer.readerIndex(), Charset.defaultCharset()), clazz);
    buffer.release();//buf       byteBuffer +buf?
    buffer = null;
    return o;
}

From source file:com.dempe.chat.common.mqtt.codec.ConnectEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, ConnectMessage message, ByteBuf out) {
    ByteBuf staticHeaderBuff = chc.alloc().buffer(12);
    ByteBuf buff = chc.alloc().buffer();
    ByteBuf variableHeaderBuff = chc.alloc().buffer(12);
    try {/*  w  ww .ja v a2 s .c  o m*/
        staticHeaderBuff.writeBytes(Utils.encodeString("MQIsdp"));

        //version 
        staticHeaderBuff.writeByte(0x03);

        //connection flags and Strings
        byte connectionFlags = 0;
        if (message.isCleanSession()) {
            connectionFlags |= 0x02;
        }
        if (message.isWillFlag()) {
            connectionFlags |= 0x04;
        }
        connectionFlags |= ((message.getWillQos() & 0x03) << 3);
        if (message.isWillRetain()) {
            connectionFlags |= 0x020;
        }
        if (message.isPasswordFlag()) {
            connectionFlags |= 0x040;
        }
        if (message.isUserFlag()) {
            connectionFlags |= 0x080;
        }
        staticHeaderBuff.writeByte(connectionFlags);

        //Keep alive timer
        staticHeaderBuff.writeShort(message.getKeepAlive());

        //Variable part
        if (message.getClientID() != null) {
            variableHeaderBuff.writeBytes(Utils.encodeString(message.getClientID()));
            if (message.isWillFlag()) {
                variableHeaderBuff.writeBytes(Utils.encodeString(message.getWillTopic()));
                variableHeaderBuff.writeBytes(Utils.encodeFixedLengthContent(message.getWillMessage()));
            }
            if (message.isUserFlag() && message.getUsername() != null) {
                variableHeaderBuff.writeBytes(Utils.encodeString(message.getUsername()));
                if (message.isPasswordFlag() && message.getPassword() != null) {
                    variableHeaderBuff.writeBytes(Utils.encodeFixedLengthContent(message.getPassword()));
                }
            }
        }

        int variableHeaderSize = variableHeaderBuff.readableBytes();
        buff.writeByte(AbstractMessage.CONNECT << 4);
        buff.writeBytes(Utils.encodeRemainingLength(12 + variableHeaderSize));
        buff.writeBytes(staticHeaderBuff).writeBytes(variableHeaderBuff);

        out.writeBytes(buff);
    } finally {
        staticHeaderBuff.release();
        buff.release();
        variableHeaderBuff.release();
    }
}

From source file:com.dempe.chat.common.mqtt.codec.PubAckEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, PubAckMessage msg, ByteBuf out) {
    ByteBuf buff = chc.alloc().buffer(4);
    try {// ww w  .  j  a va 2  s.c  o  m
        buff.writeByte(AbstractMessage.PUBACK << 4);
        buff.writeBytes(Utils.encodeRemainingLength(2));
        buff.writeShort(msg.getMessageID());
        out.writeBytes(buff);
    } finally {
        buff.release();
    }
}

From source file:com.dempe.chat.common.mqtt.codec.PublishEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, PublishMessage message, ByteBuf out) {
    if (message.getQos() == AbstractMessage.QOSType.RESERVED) {
        throw new IllegalArgumentException("Found a message with RESERVED Qos");
    }//w w w.  j  av a2 s  . c  om
    if (message.getTopicName() == null || message.getTopicName().isEmpty()) {
        throw new IllegalArgumentException("Found a message with empty or null topic name");
    }

    ByteBuf variableHeaderBuff = ctx.alloc().buffer(2);
    ByteBuf buff = null;
    try {
        variableHeaderBuff.writeBytes(Utils.encodeString(message.getTopicName()));
        if (message.getQos() == AbstractMessage.QOSType.LEAST_ONE
                || message.getQos() == AbstractMessage.QOSType.EXACTLY_ONCE) {
            if (message.getMessageID() == null) {
                throw new IllegalArgumentException("Found a message with QOS 1 or 2 and not MessageID setted");
            }
            variableHeaderBuff.writeShort(message.getMessageID());
        }
        variableHeaderBuff.writeBytes(message.getPayload());
        int variableHeaderSize = variableHeaderBuff.readableBytes();
        byte flags = Utils.encodeFlags(message);
        buff = ctx.alloc().buffer(2 + variableHeaderSize);
        buff.writeByte(AbstractMessage.PUBLISH << 4 | flags);
        buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize));
        buff.writeBytes(variableHeaderBuff);
        out.writeBytes(buff);
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        variableHeaderBuff.release();
        if (buff != null) {
            buff.release();
        }
    }
}

From source file:com.dempe.chat.common.mqtt.codec.SubAckEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, SubAckMessage message, ByteBuf out) {
    if (message.types().isEmpty()) {
        throw new IllegalArgumentException("Found a suback message with empty topics");
    }/* w  ww .j av a 2 s  .  c  om*/

    int variableHeaderSize = 2 + message.types().size();
    ByteBuf buff = chc.alloc().buffer(6 + variableHeaderSize);
    try {
        buff.writeByte(AbstractMessage.SUBACK << 4);
        buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize));
        buff.writeShort(message.getMessageID());
        for (AbstractMessage.QOSType c : message.types()) {
            buff.writeByte(c.byteValue());
        }

        out.writeBytes(buff);
    } finally {
        buff.release();
    }
}

From source file:com.dempe.chat.common.mqtt.codec.SubscribeEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, SubscribeMessage message, ByteBuf out) {
    if (message.subscriptions().isEmpty()) {
        throw new IllegalArgumentException("Found a subscribe message with empty topics");
    }/*www . j  a va 2s .co  m*/

    if (message.getQos() != AbstractMessage.QOSType.LEAST_ONE) {
        throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos());
    }

    ByteBuf variableHeaderBuff = chc.alloc().buffer(4);
    ByteBuf buff = null;
    try {
        variableHeaderBuff.writeShort(message.getMessageID());
        for (SubscribeMessage.Couple c : message.subscriptions()) {
            variableHeaderBuff.writeBytes(Utils.encodeString(c.topicFilter));
            variableHeaderBuff.writeByte(c.qos);
        }

        int variableHeaderSize = variableHeaderBuff.readableBytes();
        byte flags = Utils.encodeFlags(message);
        buff = chc.alloc().buffer(2 + variableHeaderSize);

        buff.writeByte(AbstractMessage.SUBSCRIBE << 4 | flags);
        buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize));
        buff.writeBytes(variableHeaderBuff);

        out.writeBytes(buff);
    } finally {
        variableHeaderBuff.release();
        buff.release();
    }
}

From source file:com.dempe.chat.common.mqtt.codec.UnsubscribeEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, UnsubscribeMessage message, ByteBuf out) {
    if (message.topicFilters().isEmpty()) {
        throw new IllegalArgumentException("Found an unsubscribe message with empty topics");
    }/*from w ww .  j  av a  2  s  .c  om*/

    if (message.getQos() != AbstractMessage.QOSType.LEAST_ONE) {
        throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos());
    }

    ByteBuf variableHeaderBuff = chc.alloc().buffer(4);
    ByteBuf buff = null;
    try {
        variableHeaderBuff.writeShort(message.getMessageID());
        for (String topic : message.topicFilters()) {
            variableHeaderBuff.writeBytes(Utils.encodeString(topic));
        }

        int variableHeaderSize = variableHeaderBuff.readableBytes();
        byte flags = Utils.encodeFlags(message);
        buff = chc.alloc().buffer(2 + variableHeaderSize);

        buff.writeByte(AbstractMessage.UNSUBSCRIBE << 4 | flags);
        buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize));
        buff.writeBytes(variableHeaderBuff);

        out.writeBytes(buff);
    } finally {
        variableHeaderBuff.release();
        buff.release();
    }
}

From source file:com.dempe.chat.common.netty.PublishEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, PublishMessage message, ByteBuf out) {
    if (message.getQos() == AbstractMessage.QOSType.RESERVED) {
        throw new IllegalArgumentException("Found a message with RESERVED Qos");
    }//w  ww .  j a  v  a2 s  .  c  om
    if (message.getTopicName() == null || message.getTopicName().isEmpty()) {
        throw new IllegalArgumentException("Found a message with empty or null topic name");
    }

    ByteBuf variableHeaderBuff = ctx.alloc().buffer(2);
    ByteBuf buff = null;
    try {
        variableHeaderBuff.writeBytes(Utils.encodeString(message.getTopicName()));
        if (message.getQos() == AbstractMessage.QOSType.LEAST_ONE
                || message.getQos() == AbstractMessage.QOSType.EXACTLY_ONCE) {
            if (message.getMessageID() == null) {
                throw new IllegalArgumentException("Found a message with QOS 1 or 2 and not MessageID setted");
            }
            variableHeaderBuff.writeShort(message.getMessageID());
        }
        variableHeaderBuff.writeBytes(message.getPayload());
        int variableHeaderSize = variableHeaderBuff.readableBytes();

        byte flags = Utils.encodeFlags(message);

        buff = ctx.alloc().buffer(2 + variableHeaderSize);
        buff.writeByte(AbstractMessage.PUBLISH << 4 | flags);
        buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize));
        buff.writeBytes(variableHeaderBuff);
        out.writeBytes(buff);
    } finally {
        variableHeaderBuff.release();
        if (buff != null) {
            buff.release();
        }
    }
}

From source file:com.dianping.cat.message.spi.codec.NativeMessageCodec.java

License:Open Source License

@Override
public ByteBuf encode(MessageTree tree) {
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(4 * 1024);

    try {//w w w. ja va2s . c  om
        Context ctx = new Context(tree);

        buf.writeInt(0); // place-holder

        Codec.HEADER.encode(ctx, buf, null);

        Message msg = tree.getMessage();

        if (msg != null) {
            encodeMessage(ctx, buf, msg);
        }
        int readableBytes = buf.readableBytes();

        buf.setInt(0, readableBytes - 4); // reset the message size

        return buf;
    } catch (RuntimeException e) {
        buf.release();

        throw e;
    }
}