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:io.moquette.parser.netty.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");
    }//from   w w w  .j a  v  a  2s.  co  m

    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 (QOSType c : message.types()) {
            buff.writeByte(c.byteValue());
        }

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

From source file:io.moquette.parser.netty.SubscribeDecoderTest.java

License:Open Source License

private void initMultiTopic(ByteBuf buff, int messageID, SubscribeMessage.Couple... topics)
        throws IllegalAccessException {
    ByteBuf topicBuffer = Unpooled.buffer(4);
    topicBuffer.writeShort(messageID);
    for (SubscribeMessage.Couple couple : topics) {
        topicBuffer.writeBytes(Utils.encodeString(couple.topicFilter));
        topicBuffer.writeByte(couple.qos);
    }/*  ww  w. ja  va2s  .  c o  m*/

    buff.clear().writeByte(AbstractMessage.SUBSCRIBE << 4 | (byte) 0x02)
            .writeBytes(Utils.encodeRemainingLength(topicBuffer.readableBytes()));
    buff.writeBytes(topicBuffer);
}

From source file:io.moquette.parser.netty.SubscribeDecoderTest.java

License:Open Source License

private void initPollutedTopic(ByteBuf buff, int messageID, SubscribeMessage.Couple topic)
        throws IllegalAccessException {
    ByteBuf topicBuffer = Unpooled.buffer(4);
    topicBuffer.writeShort(messageID);
    topicBuffer.writeBytes(Utils.encodeString(topic.topicFilter));
    topicBuffer.writeByte(0xF0 | topic.qos);

    buff.clear().writeByte(AbstractMessage.SUBSCRIBE << 4 | (byte) 0x02)
            .writeBytes(Utils.encodeRemainingLength(topicBuffer.readableBytes()));
    buff.writeBytes(topicBuffer);//ww  w . jav  a 2 s.co m
}

From source file:io.moquette.parser.netty.TestUtils.java

License:Open Source License

/**
 * Verify the presence of the given string starting from the current position
 * inside the buffer./*  w ww  .j  av  a 2 s. c o m*/
 */
static void verifyString(String str, ByteBuf buff) throws UnsupportedEncodingException {
    ByteBuf tmpBuff = Unpooled.buffer(2);
    byte[] raw = str.getBytes("UTF-8");
    tmpBuff.writeShort(raw.length);
    tmpBuff.writeBytes(raw);
    int buffLen = raw.length + 2;
    verifyByteBuf(tmpBuff, buff.slice(buff.readerIndex(), buffLen));
    buff.skipBytes(buffLen);
}

From source file:io.moquette.parser.netty.UnsubscribeDecoderTest.java

License:Open Source License

private void initMultiTopic(ByteBuf buff, int messageID, String... topics) throws IllegalAccessException {
    ByteBuf topicBuffer = Unpooled.buffer(4);
    topicBuffer.writeShort(messageID);
    for (String topic : topics) {
        topicBuffer.writeBytes(Utils.encodeString(topic));
    }/*  w w w.ja v a2  s.  co m*/

    buff.clear().writeByte(AbstractMessage.UNSUBSCRIBE << 4 | (byte) 0x02)
            .writeBytes(Utils.encodeRemainingLength(topicBuffer.readableBytes()));
    buff.writeBytes(topicBuffer);
}

From source file:io.netlibs.bgp.netty.protocol.BadMessageLengthNotificationPacket.java

License:Apache License

@Override
protected ByteBuf encodeAdditionalPayload() {
    final ByteBuf buffer = Unpooled.buffer(2);

    buffer.writeShort(this.getLength());

    return buffer;
}

From source file:io.netlibs.bgp.netty.protocol.BGPv4Packet.java

License:Apache License

/**
 * wrap the BGP payload in a BGPv4 header field
 *
 * @param wrapped//from  w w  w  .  j  a va  2s.com
 *          the packet payload
 * @param type
 *          the packet type code
 * @return the completely assembled BGPv4 packet
 */

private ByteBuf wrapBufferHeader(final ByteBuf wrapped, final int type) {

    final int wrappedSize = (wrapped != null) ? wrapped.readableBytes() : 0;

    final ByteBuf buffer = Unpooled.buffer(wrappedSize + BGPv4Constants.BGP_PACKET_HEADER_LENGTH);

    for (int i = 0; i < BGPv4Constants.BGP_PACKET_MARKER_LENGTH; i++) {
        buffer.writeByte(0xff);
    }

    buffer.writeShort(wrappedSize + BGPv4Constants.BGP_PACKET_HEADER_LENGTH);
    buffer.writeByte(type);

    if (wrapped != null) {
        buffer.writeBytes(wrapped);
    }

    return buffer;
}

From source file:io.netlibs.bgp.netty.protocol.MaximumNumberOfPrefixesReachedNotificationPacket.java

License:Apache License

@Override
protected ByteBuf encodeAdditionalPayload() {
    ByteBuf buffer = null;

    if (this.addressFamily != null) {
        buffer = Unpooled.buffer(7);/*  w  w w . j a va  2s  .  c om*/

        buffer.writeShort(this.addressFamily.toCode());
        buffer.writeByte(this.subsequentAddressFamily.toCode());
        buffer.writeInt(this.prefixUpperBound);
    }

    return buffer;
}

From source file:io.netlibs.bgp.netty.protocol.open.CapabilityCodec.java

License:Apache License

private static ByteBuf encodeOutboundRouteFilteringCapability(final OutboundRouteFilteringCapability cap) {
    final ByteBuf buffer = Unpooled.buffer(5 + (2 * cap.getFilters().size()));

    buffer.writeShort(cap.getAddressFamily().toCode());
    buffer.writeByte(0);/*  w  ww. j a  v  a 2 s  .c om*/
    buffer.writeByte(cap.getSubsequentAddressFamily().toCode());
    buffer.writeByte(cap.getFilters().size());

    for (final ORFType type : cap.getFilters().keySet()) {
        buffer.writeByte(type.toCode());
        buffer.writeByte(cap.getFilters().get(type).toCode());
    }

    return buffer;
}

From source file:io.netlibs.bgp.netty.protocol.open.CapabilityCodec.java

License:Apache License

private static ByteBuf encodeMultiprotocolCapability(final MultiProtocolCapability cap) {
    final ByteBuf buffer = Unpooled.buffer(4);

    if (cap.getAfi() != null) {
        buffer.writeShort(cap.getAfi().toCode());
    } else {//from www  .  j a  va2  s .  c  o  m
        buffer.writeShort(AddressFamily.RESERVED.toCode());
    }

    buffer.writeByte(0); // reserved

    if (cap.getSafi() != null) {
        buffer.writeByte(cap.getSafi().toCode());
    } else {
        buffer.writeByte(0);
    }

    return buffer;
}