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.github.stormcloud_dev.stormcloud.frame.serverbound.CreateItemServerBoundFrame.java

License:Apache License

@Override
public void writeData(ByteBuf buf) {
    super.writeData(buf);
    buf.writeShort(getX());
    buf.writeShort(getY());//from   w  w  w  .j a  v a2  s.  c o  m
    buf.writeShort(getIndex());
}

From source file:io.github.vastframework.codecs.primitives.EncodingPrimitiveSupport.java

License:Apache License

default void encodeShort(ByteBuf buffer, short value) {
    buffer.writeShort(value);
}

From source file:io.haze.transport.udp.UDPEncoder.java

License:Apache License

/**
 * Encode a {@link MessageContext} into a {@link ByteBuf}.
 *
 * @param context The channel handler context.
 * @param message The input message./*from   ww w .  j av  a  2s. c  o  m*/
 * @param buffer  The output buffer.
 */
@Override
public void encode(ChannelHandlerContext context, MessageContext message, ByteBuf buffer) throws Exception {
    ByteBuf messageBuffer = ((BufferMessage) message.getMessage()).getBuffer();
    int count = messageBuffer.readableBytes() / SAFE_PACKET_SIZE
            + ((messageBuffer.readableBytes() % SAFE_PACKET_SIZE == 0) ? 0 : 1);
    State state = (State) message.getTransportClient().getState(State.class);

    if (state == null) {
        state = new State();
    }

    try {
        int endPacketId = state.packetId - 1 + count;
        int startPacketId = state.packetId;

        while (messageBuffer.readableBytes() > 0) {
            int capacity = Math.min(SAFE_PACKET_SIZE, messageBuffer.readableBytes() + HEADERS_BYTE_SIZE);
            ByteBuf packetBuffer = Unpooled.buffer(capacity, capacity);

            // write flags, current packet id, start packet id, end packet id, payload length
            packetBuffer.writeByte(0);
            packetBuffer.writeShort(state.packetId++);
            packetBuffer.writeShort(startPacketId);
            packetBuffer.writeShort(endPacketId);
            packetBuffer.writeShort((short) (capacity - HEADERS_BYTE_SIZE));

            // read one packet worth of data from buffer and handle the packet
            messageBuffer.readBytes(packetBuffer, capacity - HEADERS_BYTE_SIZE);

            handlePacket((UDPTransportClient) message.getTransportClient(), state.packetId,
                    new DatagramPacket(packetBuffer, message.getTransportClient().getAddress()));
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        message.getTransportClient().setState(state.getClass(), state);
    }
}

From source file:io.hydramq.core.type.converters.MessagePropertiesConverter.java

License:Open Source License

@Override
public void write(final ConversionContext context, final MessageProperties properties, final ByteBuf buffer) {
    int flags = 0;
    if (properties.getStringKeys().size() > 0) {
        flags = flags | HAS_STRING_PROPERTIES;
    }/*from   w  w w.  ja  v  a  2 s.c o m*/
    if (properties.getBooleanKeys().size() > 0) {
        flags = flags | HAS_BOOLEAN_PROPERTIES;
    }
    if (properties.getIntegerKeys().size() > 0) {
        flags = flags | HAS_INTEGER_PROPERTIES;
    }
    if (properties.getLongKeys().size() > 0) {
        flags = flags | HAS_LONG_PROPERTIES;
    }
    if (properties.getFloatKeys().size() > 0) {
        flags = flags | HAS_FLOAT_PROPERTIES;
    }
    if (properties.getDoubleKeys().size() > 0) {
        flags = flags | HAS_DOUBLE_PROPERTIES;
    }
    if (properties.getBytesKeys().size() > 0) {
        flags = flags | HAS_BYTES_PROPERTIES;
    }
    if (properties.getShortKeys().size() > 0) {
        flags = flags | HAS_SHORT_PROPERTIES;
    }
    if (properties.getByteKeys().size() > 0) {
        flags = flags | HAS_BYTE_PROPERTIES;
    }
    buffer.writeInt(flags);
    if (properties.getStringKeys().size() > 0) {
        buffer.writeInt(properties.getStringKeys().size());
        for (String key : properties.getStringKeys()) {
            context.write(String.class, key, buffer);
            context.write(String.class, properties.getString(key), buffer);
        }
    }
    if (properties.getBooleanKeys().size() > 0) {
        buffer.writeInt(properties.getBooleanKeys().size());
        for (String key : properties.getBooleanKeys()) {
            context.write(String.class, key, buffer);
            buffer.writeBoolean(properties.getBoolean(key));
        }
    }
    if (properties.getIntegerKeys().size() > 0) {
        buffer.writeInt(properties.getIntegerKeys().size());
        for (String key : properties.getIntegerKeys()) {
            context.write(String.class, key, buffer);
            buffer.writeInt(properties.getInteger(key));
        }
    }
    if (properties.getLongKeys().size() > 0) {
        buffer.writeInt(properties.getLongKeys().size());
        for (String key : properties.getLongKeys()) {
            context.write(String.class, key, buffer);
            buffer.writeLong(properties.getLong(key));
        }
    }
    if (properties.getFloatKeys().size() > 0) {
        buffer.writeInt(properties.getFloatKeys().size());
        for (String key : properties.getFloatKeys()) {
            context.write(String.class, key, buffer);
            buffer.writeFloat(properties.getFloat(key));
        }
    }
    if (properties.getDoubleKeys().size() > 0) {
        buffer.writeInt(properties.getDoubleKeys().size());
        for (String key : properties.getDoubleKeys()) {
            context.write(String.class, key, buffer);
            buffer.writeDouble(properties.getDouble(key));
        }
    }
    if (properties.getBytesKeys().size() > 0) {
        buffer.writeInt(properties.getBytesKeys().size());
        for (String key : properties.getBytesKeys()) {
            context.write(String.class, key, buffer);
            context.write(byte[].class, properties.getBytes(key), buffer);
        }
    }
    if (properties.getShortKeys().size() > 0) {
        buffer.writeInt(properties.getShortKeys().size());
        for (String key : properties.getShortKeys()) {
            context.write(String.class, key, buffer);
            buffer.writeShort(properties.getShort(key));
        }
    }
    if (properties.getByteKeys().size() > 0) {
        buffer.writeInt(properties.getByteKeys().size());
        for (String key : properties.getByteKeys()) {
            context.write(String.class, key, buffer);
            buffer.writeByte(properties.getByte(key));
        }
    }
}

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

License:Open Source License

private void initHeader(ByteBuf buff, int messageID) {
    buff.clear().writeByte(AbstractMessage.PUBACK << 4).writeByte(2);

    //return code
    buff.writeShort(messageID);
}

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

License:Open Source License

private void initHeaderWithMessageID(ByteBuf buff, int messageID) throws IllegalAccessException {
    ByteBuf tmp = Unpooled.buffer(4).writeBytes(Utils.encodeString("Fake Topic"));
    tmp.writeShort(messageID);
    buff.clear().writeByte(AbstractMessage.PUBLISH << 4 | 0x02) //set Qos to 1
            .writeBytes(Utils.encodeRemainingLength(tmp.readableBytes()));
    //topic name/*  w  w w. jav a 2 s .  co m*/
    buff.writeBytes(tmp);
}

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

License:Open Source License

private void initHeaderWithMessageID_Payload(ByteBuf buff, int messageID, byte[] payload)
        throws IllegalAccessException {
    ByteBuf tmp = Unpooled.buffer(4).writeBytes(Utils.encodeString("Fake Topic"));
    tmp.writeShort(messageID);
    tmp.writeBytes(payload);/*from w w w  .  j  av a  2  s  . co m*/
    buff.clear().writeByte(AbstractMessage.PUBLISH << 4 | 0x02) //set Qos to 1
            .writeBytes(Utils.encodeRemainingLength(tmp.readableBytes()));
    //topic name
    buff.writeBytes(tmp);
}

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

License:Open Source License

private void initHeaderWithMessageID_Payload(ByteBuf buff, int messageID, ByteBuffer payload)
        throws IllegalAccessException {
    ByteBuf tmp = Unpooled.buffer(4).writeBytes(Utils.encodeString("Fake Topic"));
    tmp.writeShort(messageID);
    tmp.writeBytes(payload);//from w  w  w .  j  av a  2  s.co  m
    buff.clear().writeByte(AbstractMessage.PUBLISH << 4 | 0x02) //set Qos to 1
            .writeBytes(Utils.encodeRemainingLength(tmp.readableBytes()));
    //topic name
    buff.writeBytes(tmp);
}

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

License:Open Source License

private ByteBuf preparePubclishWithQosFlags(byte flags) {
    ByteBuf buff = Unpooled.buffer(14);/*from  w  w w. j av a 2s. c om*/
    ByteBuffer payload = ByteBuffer.allocate(3).put(new byte[] { 0x0A, 0x0B, 0x0C });
    ByteBuf tmp = Unpooled.buffer(4).writeBytes(Utils.encodeString("Fake Topic"));
    tmp.writeShort(MESSAGE_ID);
    tmp.writeBytes(payload);
    buff.clear().writeByte(AbstractMessage.PUBLISH << 4 | flags) //set DUP=1 Qos to 11 => b1110
            .writeBytes(Utils.encodeRemainingLength(tmp.readableBytes()));
    //topic name
    buff.writeBytes(tmp);

    return buff;
}

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

License:Open Source License

private void initHeaderQos(ByteBuf buff, int messageID, AbstractMessage.QOSType... qoss)
        throws IllegalAccessException {
    buff.clear().writeByte(AbstractMessage.SUBACK << 4)
            .writeBytes(Utils.encodeRemainingLength(2 + qoss.length));

    buff.writeShort(messageID);
    for (AbstractMessage.QOSType qos : qoss) {
        buff.writeByte(qos.byteValue());
    }/*from www.  j  a v a 2s. co  m*/
}