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:gedi.remote.codec.NumberEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Number msg, ByteBuf out) throws Exception {
    if (msg instanceof Byte) {
        out.writeInt(Integer.BYTES + 1 + Byte.BYTES);
        out.writeInt(1);//from w w w  .  ja  va  2 s.  com
        out.writeByte('B');
        out.writeByte(msg.byteValue());
    } else if (msg instanceof Short) {
        out.writeInt(Integer.BYTES + 1 + Short.BYTES);
        out.writeInt(1);
        out.writeByte('S');
        out.writeShort(msg.shortValue());
    } else if (msg instanceof Integer) {
        out.writeInt(Integer.BYTES + 1 + Integer.BYTES);
        out.writeInt(1);
        out.writeByte('I');
        out.writeInt(msg.intValue());
    } else if (msg instanceof Long) {
        out.writeInt(Integer.BYTES + 1 + Long.BYTES);
        out.writeInt(1);
        out.writeByte('L');
        out.writeLong(msg.longValue());
    } else if (msg instanceof Float) {
        out.writeInt(Integer.BYTES + 1 + Float.BYTES);
        out.writeInt(1);
        out.writeByte('F');
        out.writeFloat(msg.floatValue());
    } else if (msg instanceof Double) {
        out.writeInt(Integer.BYTES + 1 + Double.BYTES);
        out.writeInt(1);
        out.writeByte('D');
        out.writeDouble(msg.doubleValue());
    } else
        throw new RuntimeException("Could not encode number " + msg.getClass().getName());

}

From source file:gwlpr.mapshard.views.InstanceLoadView.java

License:Open Source License

public static void sendGuiData(Channel channel) {
    // TODO fixme!

    short[] data = new short[] { 0x66, 0xE4, 0xA6, 0xFE, 0xE7, 0x5D, 0x0C, 0x66, 0x3A, 0x3F, 0xFB, 0x11, 0x50,
            0xE3, 0xFC, 0x0D, 0xA0, 0xEE, 0x06, 0x7B, 0x74, 0xB3, 0xDB, 0xFD, 0xFF, 0x0D, 0xC8, 0xDF, 0x22,
            0x58, 0xBE, 0xD7, 0xBF, 0x9C, 0xB3, 0x8E, 0xF7, 0xFB, 0xFE, 0x63, 0x4C, 0x7C, 0xDB, 0xB4, 0x8D,
            0x4C, 0x09, 0x55, 0x43, 0xDD, 0x00, 0x18, 0x00, 0x07, 0x1B, 0x46, 0xEC, 0x6F, 0x5A, 0x3D, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x94, 0x97, 0xDF, 0x03, 0x71, 0x60, 0x8B, 0x57, 0x01, 0x01, 0x20, 0x0C, 0x82,
            0x50, 0xD3, 0x00, 0x04, 0xC0, 0x8C, 0x2A, 0x56, 0x81, 0x40, 0x41, 0x10, 0x20, 0xE8, 0xA4, 0x00,
            0x00, 0x00, 0x5C, 0x9D, 0x21, 0x38, 0x42, 0x40, 0x00, 0x60, 0x10, 0x68, 0x01, 0x00, 0x2C, 0x4A,
            0x29, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x90, 0x00, 0x28, 0xCC, 0xE2, 0x76, 0xC0, 0x3A,
            0x2E, 0x3A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x10, 0x05, 0x11, 0x39, 0xCC, 0x78,
            0x00, 0x9A, 0x93, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xDF, 0xB2, 0x25, 0xD0, 0x2F,
            0xFD, 0xE7, 0x85, 0xB8, 0xBD, 0xF8, 0x30, 0x20, 0x16, 0xA3, 0x48, 0x2B, 0x00, 0x00, 0x00, 0x60,
            0xC6, 0xCE, 0x44, 0x00, 0x00, 0xC0, 0x0E, 0x22, 0x00, 0x00, 0x85, 0x71, 0xD7, 0x71, 0xA1, 0xDC,
            0x6B, 0x7B, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0x09, 0x5C, 0x18, 0x03,
            0x00, 0x00, 0x00, 0x00, 0xE5, 0xB3, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x59, 0x50,
            0x00, 0x00, 0x00, 0x00, 0xA0, 0x4C, 0x65, 0x01, 0x00, 0x00, 0x40, 0x0D, 0x00, 0x01, 0x00, 0xFA,
            0x4E, 0xAD, 0x0F };//from  w  w  w .j a va  2  s.  c om

    ByteBuf buf = channel.alloc().buffer(2 + 2 + data.length).order(ByteOrder.LITTLE_ENDIAN);

    // write the header
    buf.writeShort(18);

    // write length of the data in ints
    buf.writeShort(data.length / 4);

    for (int i = 0; i < data.length; i++) {
        buf.writeByte(data[i]);
    }

    channel.writeAndFlush(buf);
}

From source file:gwlpr.protocol.NettyGWCodec.java

License:Open Source License

@Override
public void encode(ChannelHandlerContext ctx, GWMessage message, ByteBuf out) {
    ByteBuf result = out.order(ByteOrder.LITTLE_ENDIAN);

    LOGGER.debug("Put: {}", message.toString());

    GWMessage gwact = message;/*from w w  w. ja  v a 2  s.  co m*/
    int header = gwact.getHeader();

    // try retrieve the serialization filter
    NettySerializationFilter filter = GWMessageSerializationRegistry.getFilter(gwact.getClass());

    if (filter == null) {
        LOGGER.error("Could not find a filter for given message.");
        return;
    }

    // write the header
    result.writeShort(header);

    // serialize the message
    filter.serialize(result, message);
}

From source file:hellfirepvp.astralsorcery.common.network.packet.server.PktSyncConfig.java

License:Open Source License

@Override
public void toBytes(ByteBuf buf) {
    List<byte[]> bufferedFieldContents = new ArrayList<>();
    for (Field f : Config.class.getFields()) {
        if (Modifier.isStatic(f.getModifiers()) && f.isAnnotationPresent(Sync.class)) {
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            try {
                new DataOutputStream(byteStream).writeUTF(f.getName());
                new ObjectOutputStream(byteStream).writeObject(f.get(null));
            } catch (Exception ignored) {
            }//from  w w  w. j av a 2  s.  com
            bufferedFieldContents.add(byteStream.toByteArray());
            try {
                byteStream.close();
            } catch (IOException ignored) {
            }
        }
    }

    buf.writeByte(bufferedFieldContents.size());
    for (byte[] data : bufferedFieldContents) {
        buf.writeShort(data.length);
        buf.writeBytes(data);
    }
}

From source file:hivemall.mix.MixMessageEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, MixMessage msg, ByteBuf out) throws Exception {
    int startIdx = out.writerIndex();
    out.writeBytes(LENGTH_PLACEHOLDER);/*from  w  w  w . j  a v  a  2  s.  c om*/

    MixEventName event = msg.getEvent();
    byte b = event.getID();
    out.writeByte(b);

    Object feature = msg.getFeature();
    encodeObject(feature, out);

    float weight = msg.getWeight();
    out.writeFloat(weight);

    float covariance = msg.getCovariance();
    out.writeFloat(covariance);

    short clock = msg.getClock();
    out.writeShort(clock);

    int deltaUpdates = msg.getDeltaUpdates();
    out.writeInt(deltaUpdates);

    boolean cancelRequest = msg.isCancelRequest();
    out.writeBoolean(cancelRequest);

    String groupId = msg.getGroupID();
    writeString(groupId, out);

    int endIdx = out.writerIndex();
    out.setInt(startIdx, endIdx - startIdx - 4);
}

From source file:impl.underdark.transport.bluetooth.discovery.ble.transport.peripheral.BlePeripheral.java

License:Open Source License

private void describeCharacteristicFormat(BluetoothDevice device, int requestId, int offset,
        BluetoothGattDescriptor descriptor) {
    // https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorViewer.aspx?u=org.bluetooth.descriptor.gatt.characteristic_presentation_format.xml
    // https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/file/ecbc3405c66e/public/GattCharacteristic.h
    byte format = 10;
    if (BleConfig.charactAddressUuid.equals(descriptor.getCharacteristic().getUuid()))
        format = 25;/* w w  w  .j a  v a2 s .c o m*/
    if (BleConfig.charactNodeIdUuid.equals(descriptor.getCharacteristic().getUuid()))
        format = 18;

    ByteBuf buffer = Unpooled.buffer();
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.writeByte(format); // format
    buffer.writeByte(0); // exponent
    buffer.writeShort(0x2700); // unit
    buffer.writeByte(1); // namespace
    buffer.writeShort(0); // description

    if (offset >= buffer.readableBytes()) {
        Logger.warn("ble peripheral format charact failed - invalid offset {}", offset);
        gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_INVALID_OFFSET, offset, null);
        return;
    }

    byte[] value = Arrays.copyOfRange(buffer.array(), offset,
            Math.min(buffer.readableBytes() - offset, BleConfig.charactValueSizeMax));
    gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
}

From source file:io.advantageous.conekt.dns.impl.netty.DnsQueryEncoder.java

License:Open Source License

/**
 * Encodes the information in a {@link DnsQueryHeader} and writes it to the
 * specified {@link io.netty.buffer.ByteBuf}. The header is always 12 bytes long.
 *
 * @param header the query header being encoded
 * @param buf    the buffer the encoded data should be written to
 *///from  ww  w  .  jav a2s .  co m
private static void encodeHeader(DnsQueryHeader header, ByteBuf buf) {
    buf.writeShort(header.getId());
    int flags = 0;
    flags |= header.getType() << 15;
    flags |= header.getOpcode() << 14;
    flags |= header.isRecursionDesired() ? 1 << 8 : 0;
    buf.writeShort(flags);
    buf.writeShort(header.questionCount());
    buf.writeShort(header.answerCount()); // Must be 0
    buf.writeShort(header.authorityResourceCount()); // Must be 0
    buf.writeShort(header.additionalResourceCount()); // Must be 0
}

From source file:io.advantageous.conekt.dns.impl.netty.DnsQueryEncoder.java

License:Open Source License

/**
 * Encodes the information in a {@link DnsQuestion} and writes it to the
 * specified {@link io.netty.buffer.ByteBuf}.
 *
 * @param question the question being encoded
 * @param charset  charset names are encoded in
 * @param buf      the buffer the encoded data should be written to
 *///  w ww  . j a  v a  2 s  .  co  m
private static void encodeQuestion(DnsQuestion question, Charset charset, ByteBuf buf) {
    String[] parts = question.name().split("\\.");
    for (int i = 0; i < parts.length; i++) {
        buf.writeByte(parts[i].length());
        buf.writeBytes(charset.encode(parts[i]));
    }
    buf.writeByte(0); // marks end of name field
    buf.writeShort(question.type());
    buf.writeShort(question.dnsClass());
}

From source file:io.airlift.drift.transport.netty.codec.HeaderTransport.java

License:Apache License

/**
 * Encodes the HeaderFrame into a ByteBuf transferring the reference ownership.
 * @param frame frame to be encoded; reference count ownership is transferred to this method
 * @return the encoded frame data; caller is responsible for releasing this buffer
 *//*from  w  w w .ja  v a 2 s. co m*/
public static ByteBuf encodeFrame(ThriftFrame frame) {
    try {
        // describe the encoding (Thrift protocol, compression info)
        ByteBuf encodingInfo = Unpooled.buffer(3);
        encodingInfo.writeByte(frame.getProtocol().getHeaderTransportId());
        // number of "transforms" -- no transforms are supported
        encodingInfo.writeByte(0);

        // headers
        ByteBuf encodedHeaders = encodeHeaders(frame.getHeaders());

        // Padding - header size must be a multiple of 4
        int headerSize = encodingInfo.readableBytes() + encodedHeaders.readableBytes();
        ByteBuf padding = getPadding(headerSize);
        headerSize += padding.readableBytes();

        // frame header (magic, flags, sequenceId, headerSize
        ByteBuf frameHeader = Unpooled.buffer(FRAME_HEADER_SIZE);
        frameHeader.writeShort(HEADER_MAGIC);
        frameHeader.writeShort(frame.isSupportOutOfOrderResponse() ? FLAG_SUPPORT_OUT_OF_ORDER : FLAGS_NONE);
        frameHeader.writeInt(frame.getSequenceId());
        frameHeader.writeShort(headerSize >> 2);

        // header frame is a simple wrapper around the frame method, so the frame does not need to be released
        return Unpooled.wrappedBuffer(frameHeader, encodingInfo, encodedHeaders, padding, frame.getMessage());
    } finally {
        frame.release();
    }
}

From source file:io.airlift.drift.transport.netty.HeaderMessageEncoding.java

License:Apache License

@Override
public ByteBuf writeRequest(ByteBufAllocator allocator, int sequenceId, MethodMetadata method,
        List<Object> parameters, Map<String, String> headers) throws Exception {
    ByteBuf message = MessageEncoding.encodeRequest(protocolFactory, allocator, sequenceId, method, parameters);

    ////from w  w  w  . j a v  a  2s.c  o m
    // describe the encoding (Thrift protocol, compression info)
    ByteBuf encodingInfo = Unpooled.buffer(3);
    encodingInfo.writeByte(protocolId);
    // number of "transforms"
    encodingInfo.writeByte(gzip ? 1 : 0);
    if (gzip) {
        // Note this is actually a vint, but there are only 3 headers for now
        encodingInfo.writeByte(0x01);
    }

    // headers
    ByteBuf encodedHeaders = encodeHeaders(headers);

    // Padding - header size must be a multiple of 4
    int headerSize = encodingInfo.readableBytes() + encodedHeaders.readableBytes();
    ByteBuf padding = getPadding(headerSize);
    headerSize += padding.readableBytes();

    // frame header (magic, flags, sequenceId, headerSize
    ByteBuf frameHeader = Unpooled.buffer(12);
    frameHeader.writeShort(HEADER_MAGIC);
    frameHeader.writeShort(FLAG_SUPPORT_OUT_OF_ORDER);
    frameHeader.writeInt(sequenceId);
    frameHeader.writeShort(headerSize >> 2);

    return Unpooled.wrappedBuffer(frameHeader, encodingInfo, encodedHeaders, padding, message);
}