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.rs3e.utility.ByteBufUtils.java

License:Open Source License

public static void writeSmart(ByteBuf buffer, int value) {
    if (value < 128)
        buffer.writeByte(value);/*w w w.  ja  v a 2 s .  c  o  m*/
    else
        buffer.writeShort(32768 + value);
}

From source file:com.splicemachine.stream.KryoEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Object in, ByteBuf out) throws Exception {
    outStream.reset();//  ww  w . ja  v a  2s .  c o m

    Kryo encoder = kp.get();
    try {
        encoder.writeClassAndObject(output, in);
    } finally {
        kp.returnInstance(encoder);
    }

    output.flush();
    byte[] outArray = outStream.toByteArray();
    out.writeShort(outArray.length);
    out.writeBytes(outArray);
}

From source file:com.spotify.folsom.client.binary.BinaryMemcacheDecoderTest.java

License:Apache License

@Test
public void test() throws Exception {
    GetRequest request = new GetRequest(KEY, OpCode.GET, 123, OPAQUE);
    BinaryMemcacheDecoder decoder = new BinaryMemcacheDecoder();

    ByteBuf cb = Unpooled.buffer(30);
    cb.writeByte(0x81);//from   ww w  .j a  v a  2s. c om
    cb.writeByte(OpCode.GET);
    cb.writeShort(3);
    cb.writeByte(0);
    cb.writeZero(1);
    cb.writeShort(0);
    cb.writeInt(6);
    cb.writeInt(request.getOpaque());
    cb.writeLong(258);
    cb.writeBytes(KEY.getBytes());
    cb.writeBytes(VALUE.getBytes());

    List<Object> out = Lists.newArrayList();
    decoder.decode(null, cb, out);
    @SuppressWarnings("unchecked")
    List<ResponsePacket> replies = (List<ResponsePacket>) out.get(0);
    request.handle(replies);

    GetResult<byte[]> getResult = request.get();
    assertEquals(258, getResult.getCas());
    assertEquals(VALUE, TRANSCODER.decode(getResult.getValue()));
}

From source file:com.streamsets.pipeline.lib.parser.net.netflow.NetflowTestUtil.java

License:Apache License

public static void writeV5NetflowHeader(EmbeddedChannel channel, int count, long uptime, long seconds,
        long nanos, long flowSequence, int engineType, int engineId, int sampling) {
    final int version = 5;

    final ByteBuf buf = Unpooled.buffer();
    buf.writeShort(version);
    buf.writeShort(count);/*from  www  .ja va2 s.c o m*/
    buf.writeInt((int) uptime);
    buf.writeInt((int) seconds);
    buf.writeInt((int) nanos);
    buf.writeInt((int) flowSequence);
    buf.writeByte(engineType);
    buf.writeByte(engineId);
    buf.writeShort(sampling);

    channel.writeInbound(buf);
}

From source file:com.streamsets.pipeline.lib.parser.net.netflow.NetflowTestUtil.java

License:Apache License

public static void writeV5NetflowFlowRecord(EmbeddedChannel channel, long srcAddr, long destAddr, long nextHop,
        int snmpInput, int snmpOutput, long packets, long octets, long first, long last, int srcPort,
        int destPort, int tcpFlags, int proto, int tos, int srcAs, int destAs, int srcMask, int destMask

) {//from   w  ww. j a va  2s.  co m

    final ByteBuf buf = Unpooled.buffer();
    buf.writeInt((int) srcAddr);
    buf.writeInt((int) destAddr);
    buf.writeInt((int) nextHop);
    buf.writeShort(snmpInput);
    buf.writeShort(snmpOutput);
    buf.writeInt((int) packets);
    buf.writeInt((int) octets);
    buf.writeInt((int) first);
    buf.writeInt((int) last);
    buf.writeShort(srcPort);
    buf.writeShort(destPort);
    // one empty pad byte
    buf.writeByte(0);
    buf.writeByte(tcpFlags);
    buf.writeByte(proto);
    buf.writeByte(tos);
    buf.writeShort(srcAs);
    buf.writeShort(destAs);
    buf.writeByte(srcMask);
    buf.writeByte(destMask);
    // two empty pad bytes
    buf.writeByte(0);
    buf.writeByte(0);
    channel.writeInbound(buf);
}

From source file:com.streamsets.pipeline.lib.parser.netflow.TestNetflowParser.java

License:Apache License

@Test(expected = OnRecordErrorException.class)
public void testInvalidVersion() throws Exception {
    UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    NetflowParser netflowParser = new NetflowParser(getContext());
    ByteBuf buf = allocator.buffer(4);
    buf.writeShort(0);
    buf.writeShort(0);//from  w w  w . j a  v  a  2  s.  co m
    netflowParser.parse(buf, null, null);
}

From source file:com.streamsets.pipeline.lib.parser.netflow.TestNetflowParser.java

License:Apache License

@Test(expected = OnRecordErrorException.class)
public void testInvalidCountInvalidLength() throws Exception {
    UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    NetflowParser netflowParser = new NetflowParser(getContext());
    ByteBuf buf = allocator.buffer(4);
    buf.writeShort(5);
    buf.writeShort(1);//from   ww  w .  j  a  v a  2s.com
    netflowParser.parse(buf, null, null);
}

From source file:com.streamsets.pipeline.lib.parser.netflow.TestNetflowParser.java

License:Apache License

@Test(expected = OnRecordErrorException.class)
public void testInvalidCountZero() throws Exception {
    UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    NetflowParser netflowParser = new NetflowParser(getContext());
    ByteBuf buf = allocator.buffer(4);
    buf.writeShort(5);
    buf.writeShort(0);/*from   w w w .ja v a2s .  co m*/
    netflowParser.parse(buf, null, null);
}

From source file:com.streamsets.pipeline.lib.parser.netflow.TestNetflowParser.java

License:Apache License

@Test(expected = OnRecordErrorException.class)
public void testInvalidPacketTooShort2() throws Exception {
    UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    NetflowParser netflowParser = new NetflowParser(getContext());
    ByteBuf buf = allocator.buffer(2);
    buf.writeShort(5);
    netflowParser.parse(buf, null, null);
}

From source file:com.streamsets.pipeline.lib.parser.udp.netflow.TestNetflowParser.java

License:Apache License

@Test(expected = OnRecordErrorException.class)
public void testInvalidVersion() throws Exception {
    UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    NetflowParser netflowParser = makeNetflowParser();
    ByteBuf buf = allocator.buffer(4);
    buf.writeShort(0);
    buf.writeShort(0);/*from   w  w  w  .  j  a  va  2 s .co  m*/
    netflowParser.parse(buf, null, null);
}