Example usage for java.nio ByteBuffer putShort

List of usage examples for java.nio ByteBuffer putShort

Introduction

In this page you can find the example usage for java.nio ByteBuffer putShort.

Prototype

public abstract ByteBuffer putShort(short value);

Source Link

Document

Writes the given short to the current position and increases the position by 2.

Usage

From source file:de.rwhq.serializer.FixedStringSerializer.java

@Override
public byte[] serialize(final String o) {
    final byte[] bytes = o.getBytes();

    if (bytes.length > (length - 1)) {
        throw new IllegalArgumentException("String is too long to be serialized");
    }//from   ww w.  j  a v  a2  s .  c  o m

    final ByteBuffer buf = ByteBuffer.allocate(length);
    buf.putShort((short) bytes.length);
    buf.put(bytes);
    return buf.array();
}

From source file:org.dragonet.entity.metadata.type.ByteArrayMeta.java

@Override
public byte[] encode() {
    ByteBuffer buff = ByteBuffer.allocate(2 + this.data.length);
    buff.order(ByteOrder.LITTLE_ENDIAN);
    buff.putShort((short) (this.data.length & 0xFFFF));
    buff.put(this.data);
    return buff.array();
}

From source file:net.jenet.Header.java

public void toBuffer(ByteBuffer buffer) {
    buffer.putShort(peerID);
    buffer.put(flags);/*www  . j  a v a2  s. co  m*/
    buffer.put(commandCount);
    buffer.putInt(sentTime);
    buffer.putInt(challenge);
}

From source file:com.spotify.heroic.metric.datastax.schema.legacy.MapSerializer.java

@Override
public ByteBuffer serialize(final Map<A, B> value) throws IOException {
    final List<Pair<ByteBuffer, ByteBuffer>> buffers = new ArrayList<>();

    short size = 0;

    for (final Map.Entry<A, B> e : value.entrySet()) {
        final ByteBuffer key = a.serialize(e.getKey());
        final ByteBuffer val = b.serialize(e.getValue());

        size += key.limit() + val.limit();

        buffers.add(Pair.of(key, val));
    }//from  ww  w  . ja  va2  s.c  o m

    final ByteBuffer buffer = ByteBuffer.allocate(4 + 8 * value.size() + size);
    buffer.putShort((short) buffers.size());

    for (final Pair<ByteBuffer, ByteBuffer> p : buffers) {
        buffer.putShort((short) p.getLeft().remaining());
        buffer.put(p.getLeft());
        buffer.putShort((short) p.getRight().remaining());
        buffer.put(p.getRight());
    }

    buffer.flip();
    return buffer;
}

From source file:com.offbynull.portmapper.natpmp.TcpMappingNatPmpRequest.java

@Override
protected void dumpOpCodeSpecificInformation(ByteBuffer dst) {
    dst.putShort((short) 0);
    dst.putShort((short) internalPort);
    dst.putShort((short) suggestedExternalPort);
    dst.putInt((int) lifetime);
}

From source file:org.onlab.packet.ICMPTest.java

@Before
public void setUp() throws Exception {
    deserializer = ICMP.deserializer();/*from   ww  w .ja v a2s  .  c  o m*/

    ByteBuffer bb = ByteBuffer.allocate(ICMP.ICMP_HEADER_LENGTH);

    bb.put(icmpType);
    bb.put(icmpCode);
    bb.putShort(checksum);

    headerBytes = bb.array();
}

From source file:ByteString.java

public void store(ByteBuffer bb) {
    short n = 0;//ww w  . j  a v  a  2 s .co  m
    if (bytes != null) {
        n = (short) bytes.length;
    }
    bb.putShort(n);
    if (n > 0) {
        bb.put(bytes, 0, n);
    }
}

From source file:com.github.ambry.commons.BlobId.java

@Override
public byte[] toBytes() {
    ByteBuffer idBuf = ByteBuffer.allocate(sizeInBytes());
    idBuf.putShort(version);
    idBuf.put(partitionId.getBytes());/*from  w ww. jav a2s  . c o  m*/
    idBuf.putInt(uuid.getBytes().length);
    idBuf.put(uuid.getBytes());
    return idBuf.array();
}

From source file:tor.Cell.java

public byte[] getBytes(int protocolVersion) {
    byte cell[];/*from www  .j a va2s . co  m*/

    if (cmdId == 7 || cmdId >= 128)
        cell = new byte[(protocolVersion < 4 ? 3 : 5) + 2 + payload.length];
    else
        cell = new byte[protocolVersion < 4 ? 512 : 514];

    ByteBuffer buf = ByteBuffer.wrap(cell);
    buf.order(ByteOrder.BIG_ENDIAN);
    if (protocolVersion < 4)
        buf.putShort((short) circId);
    else
        buf.putInt((int) circId);
    buf.put((byte) cmdId);

    if (cmdId == 7 || cmdId >= 128)
        buf.putShort((short) payload.length);

    if (payload != null)
        buf.put(payload);
    //System.out.println("Sending:" + byteArrayToHex(cell));
    return cell;
}

From source file:org.onlab.packet.ArpTest.java

@Before
public void setUp() {
    ByteBuffer bb = ByteBuffer
            .allocate(ARP.INITIAL_HEADER_LENGTH + 2 * hwAddressLength + 2 * protoAddressLength);
    bb.putShort(ARP.HW_TYPE_ETHERNET);
    bb.putShort(ARP.PROTO_TYPE_IP);/*w w  w  . ja va 2  s.com*/
    bb.put(hwAddressLength);
    bb.put(protoAddressLength);
    bb.putShort(ARP.OP_REPLY);

    bb.put(srcMac.toBytes());
    bb.put(srcIp.toOctets());
    bb.put(targetMac.toBytes());
    bb.put(targetIp.toOctets());

    byteHeader = bb.array();
}