Example usage for io.netty.buffer ByteBuf writeShort

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

Introduction

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

Prototype

public abstract ByteBuf writeShort(int value);

Source Link

Document

Sets the specified 16-bit short integer at the current writerIndex and increases the writerIndex by 2 in this buffer.

Usage

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeStringList(List<String> l, ByteBuf cb) {
    cb.writeShort(l.size());
    for (String str : l)
        writeString(str, cb);/*from  w w w  .java 2  s .  co m*/
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeStringMap(Map<String, String> m, ByteBuf cb) {
    cb.writeShort(m.size());
    for (Map.Entry<String, String> entry : m.entrySet()) {
        writeString(entry.getKey(), cb);
        writeString(entry.getValue(), cb);
    }/* ww w .ja va 2s  .  co m*/
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeStringToStringListMap(Map<String, List<String>> m, ByteBuf cb) {
    cb.writeShort(m.size());
    for (Map.Entry<String, List<String>> entry : m.entrySet()) {
        writeString(entry.getKey(), cb);
        writeStringList(entry.getValue(), cb);
    }/*w w  w.j av  a  2  s .  c o  m*/
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeValueList(List<ByteBuffer> values, ByteBuf cb) {
    cb.writeShort(values.size());
    for (ByteBuffer value : values)
        CBUtil.writeValue(value, cb);/*from   ww  w  .ja v a  2s.c  o  m*/
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeNamedValueList(Map<String, ByteBuffer> namedValues, ByteBuf cb) {
    cb.writeShort(namedValues.size());
    for (Map.Entry<String, ByteBuffer> entry : namedValues.entrySet()) {
        CBUtil.writeString(entry.getKey(), cb);
        CBUtil.writeValue(entry.getValue(), cb);
    }// w  w w. j  a v a2  s .  c o m
}

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 {/*from   w  w  w .j a v a 2s .c om*/
        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 {//from w ww.  j  a  va2  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.PubCompEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, PubCompMessage msg, ByteBuf out) {
    out.writeByte(AbstractMessage.PUBCOMP << 4);
    out.writeBytes(Utils.encodeRemainingLength(2));
    out.writeShort(msg.getMessageID());
}

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");
    }//from  w w w  .  j  a v a  2 s .co  m
    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.PubRecEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, PubRecMessage msg, ByteBuf out) {
    out.writeByte(AbstractMessage.PUBREC << 4);
    out.writeBytes(Utils.encodeRemainingLength(2));
    out.writeShort(msg.getMessageID());
}