Example usage for java.lang Short BYTES

List of usage examples for java.lang Short BYTES

Introduction

In this page you can find the example usage for java.lang Short BYTES.

Prototype

int BYTES

To view the source code for java.lang Short BYTES.

Click Source Link

Document

The number of bytes used to represent a short value in two's complement binary form.

Usage

From source file:ru.jts_dev.common.packets.IncomingMessageWrapper.java

public final int readShort() {
    if (payload.readableBytes() < Short.BYTES)
        throw new IndexOutOfBoundsException("At least 2 bytes must be readable in payload");

    return payload.readShort();
}

From source file:org.onlab.util.ImmutableByteSequence.java

/**
 * Creates a new byte sequence of 2 bytes containing the given short value.
 *
 * @param original a short value/* ww  w.j a  v a 2 s. c  o  m*/
 * @return a new immutable byte sequence
 */
public static ImmutableByteSequence copyFrom(short original) {
    return new ImmutableByteSequence(ByteBuffer.allocate(Short.BYTES).putShort(original));
}

From source file:edu.umass.cs.gigapaxos.paxospackets.RequestPacket.java

static int sizeof(Class<?> clazz) {
    switch (clazz.getSimpleName()) {
    case "int":
        return Integer.BYTES;
    case "long":
        return Long.BYTES;
    case "boolean":
        return 1;
    case "short":
        return Short.BYTES;
    case "InetSocketAddress":
        return Integer.BYTES + Short.BYTES;
    }//www . j  av a2  s . c  om
    assert (false) : clazz;
    return -1;
}

From source file:edu.umass.cs.gigapaxos.paxospackets.RequestPacket.java

/**
 * The weird constant above is to try to avoid mistakes in the painful (but
 * totally worth it) byte'ification method below. Using bytes as opposed to
 * json strings makes a non-trivial difference (~2x over json-smart and >4x
 * over org.json. So we just chuck json libraries and use our own byte[]
 * serializer for select packets.//from   w ww  .ja va2  s .c o  m
 * 
 * The serialization overhead really matters most for RequestPacket and
 * AcceptPacket. Every request, even with batching, must be deserialized by
 * the coordinator and must be serialized back while sending out the
 * AcceptPacket. The critical path is the following at a coordinator and is
 * incurred at least in part even with batching for every request: (1)
 * receive request, (2) send accept, (3) receive accept_replies, (4) send
 * commit Accordingly, we use byteification for {@link RequestPacket},
 * {@link AcceptPacket}, {@link BatchedAcceptReply} and
 * {@link BatchedCommit}.
 * 
 * */

protected byte[] toBytes(boolean instrument) {
    // return cached value if already present
    if ((this.getType() == PaxosPacketType.REQUEST || this.getType() == PaxosPacketType.ACCEPT)
            && this.byteifiedSelf != null && !instrument)
        return this.byteifiedSelf;
    // check if we can use byteification at all; if not, use toString()
    if (!((BYTEIFICATION && IntegerMap.allInt()) || instrument)) {
        try {
            if (this.getType() == PaxosPacketType.REQUEST || this.getType() == PaxosPacketType.ACCEPT)
                return this.byteifiedSelf = this.toString().getBytes(CHARSET); // cache
            return this.toString().getBytes(CHARSET);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
            return null;
        }
    }

    // else byteify
    try {
        int exactLength = 0;
        byte[] array = new byte[this.lengthEstimate()];
        ByteBuffer bbuf = ByteBuffer.wrap(array);
        assert (bbuf.position() == 0);

        // paxospacket stuff
        super.toBytes(bbuf);
        int ppPos = bbuf.position(); // for assertion
        assert (bbuf.position() == ByteBuffer.wrap(array, SIZEOF_PAXOSPACKET_FIXED - 1, 1).get()
                + SIZEOF_PAXOSPACKET_FIXED) : bbuf.position() + " != "
                        + ByteBuffer.wrap(array, SIZEOF_PAXOSPACKET_FIXED - 1, 1).get()
                        + SIZEOF_PAXOSPACKET_FIXED;
        exactLength += (bbuf.position());

        bbuf.putLong(this.requestID);
        bbuf.put(this.stop ? (byte) 1 : (byte) 0);
        exactLength += (Long.BYTES + 1);

        // addresses
        /* Note: 0 is ambiguous with wildcard address, but that's okay
         * because an incoming packet will never come with a wildcard
         * address. */
        bbuf.put(this.clientAddress != null ? this.clientAddress.getAddress().getAddress() : new byte[4]);
        // 0 (not -1) means invalid port
        bbuf.putShort(this.clientAddress != null ? (short) this.clientAddress.getPort() : 0);
        /* Note: 0 is an ambiguous wildcard address that could also be a
         * legitimate value of the listening socket address. If the request
         * happens to have no listening address, we will end up assuming it
         * was received on the wildcard address. At worst, the matching for
         * the corresponding response back to the client can fail. */
        bbuf.put(this.listenAddress != null ? this.listenAddress.getAddress().getAddress() : new byte[4]);
        // 0 (not -1) means invalid port
        bbuf.putShort(this.listenAddress != null ? (short) this.listenAddress.getPort() : 0);
        exactLength += 2 * (Integer.BYTES + Short.BYTES);

        // other non-final fields
        bbuf.putInt(this.entryReplica);
        bbuf.putLong(this.entryTime);
        bbuf.put(this.shouldReturnRequestValue ? (byte) 1 : (byte) 0);
        bbuf.putInt(this.forwardCount);
        exactLength += (Integer.BYTES + Long.BYTES + 1 + Integer.BYTES);

        // digest related fields: broadcasted, digest
        // whether this request was already broadcasted
        bbuf.put(this.broadcasted ? (byte) 1 : (byte) 0);
        exactLength += 1;
        assert (exactLength ==
        // where parent left us off
        ppPos + SIZEOF_REQUEST_FIXED
        // for the three int fields not yet filled
                - 4 * Integer.BYTES) : exactLength + " != [" + ppPos + " + " + SIZEOF_REQUEST_FIXED + " - "
                        + 4 * Integer.BYTES + "]";
        // digest length and digest iteself
        bbuf.putInt(this.digest != null ? this.digest.length : 0);
        exactLength += Integer.BYTES;
        if (this.digest != null)
            bbuf.put(this.digest);
        exactLength += (this.digest != null ? this.digest.length : 0);
        // /////////// end of digest related fields //////////

        // highly variable length fields
        // requestValue
        byte[] reqValBytes = this.requestValue != null ? this.requestValue.getBytes(CHARSET) : new byte[0];
        bbuf.putInt(reqValBytes != null ? reqValBytes.length : 0);
        bbuf.put(reqValBytes);
        exactLength += (4 + reqValBytes.length);

        // responseValue
        byte[] respValBytes = this.responseValue != null ? this.responseValue.getBytes(CHARSET) : new byte[0];
        bbuf.putInt(respValBytes != null ? respValBytes.length : 0);
        bbuf.put(respValBytes);
        exactLength += (4 + respValBytes.length);

        // batched requests batchSize|(length:batchedReqBytes)+
        bbuf.putInt(this.batchSize());
        exactLength += (4);
        if (this.batchSize() > 0)
            for (RequestPacket req : this.batched) {
                byte[] element = req.toBytes();
                bbuf.putInt(element.length);
                bbuf.put(element);
                exactLength += (4 + element.length);
            }

        // bbuf.array() was a generous allocation
        byte[] exactBytes = new byte[exactLength];
        bbuf.flip();
        assert (bbuf.remaining() == exactLength) : bbuf.remaining() + " != " + exactLength;
        bbuf.get(exactBytes);

        if (this.getType() == PaxosPacketType.REQUEST)
            this.byteifiedSelf = exactBytes;

        return exactBytes;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}