Example usage for com.google.common.primitives Shorts toByteArray

List of usage examples for com.google.common.primitives Shorts toByteArray

Introduction

In this page you can find the example usage for com.google.common.primitives Shorts toByteArray.

Prototype

@GwtIncompatible("doesn't work")
public static byte[] toByteArray(short value) 

Source Link

Document

Returns a big-endian representation of value in a 2-element byte array; equivalent to ByteBuffer.allocate(2).putShort(value).array() .

Usage

From source file:com.prealpha.minelib.nbt.ShortTag.java

@Override
public ByteBuffer toBytes() {
    return ByteBuffer.wrap(Shorts.toByteArray(value));
}

From source file:com.yandex.yoctodb.util.UnsignedByteArrays.java

@NotNull
public static UnsignedByteArray from(final short s) {
    return from(Shorts.toByteArray((short) (s ^ Short.MIN_VALUE)));
}

From source file:org.opendaylight.dhcpv6.server.Dhcpv6ServerDaemon.java

public Dhcpv6ServerDaemon(final DataBroker dataBroker, final EventLoopGroup eventLoopGroup,
        final Dhcp6LeaseManager manager, short serverId, final DhcpServerCfg dhcpServerCfg) {
    this.dataBroker = dataBroker;
    this.eventLoopGroup = eventLoopGroup;
    final int port = dhcpServerCfg.getPort().getValue();
    final List<String> networkInterfaces = dhcpServerCfg.getNetworkInterface();
    dhcpService = new CustomisableLeaseManagerDhcpv6Service(manager, Shorts.toByteArray(serverId),
            dhcpServerCfg.getDefaultOption());
    try {//from  www.  j a  v  a  2 s .co  m
        dhcpServer = new Dhcpv6Server(dhcpService, serverId, (short) port,
                new HashSet<String>(networkInterfaces));
    } catch (SocketException | IllegalArgumentException e) {
        LOG.error("DHCP server on port {} failed to decode option arguments {}", port, e);
        throw new IllegalStateException(e);
    }
}

From source file:org.opendaylight.dhcpv6.server.Dhcpv6Server.java

public Dhcpv6Server(Dhcp6Service dhcpService, short serverDuid, short port, Set<String> interfaceNames)
        throws SocketException {
    this.handler = new Dhcp6Handler(dhcpService, Shorts.toByteArray(serverDuid));

    this.networkInterfaces = new ArrayList<NetworkInterface>();
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    NetworkInterface networkInterface;
    while (networkInterfaces.hasMoreElements()) {
        networkInterface = networkInterfaces.nextElement();
        if (interfaceNames.isEmpty() || interfaceNames.contains(networkInterface.getName())) {
            this.networkInterfaces.add(networkInterface);
        }// w  ww  .  ja v  a  2 s  . c  o  m
    }

    this.port = port;
}

From source file:org.apache.directory.server.dhcp.options.ShortOption.java

public void setShortValue(@Nonnegative int value) {
    Preconditions.checkArgument(value >> Short.SIZE == 0, "out of range: %s", value);
    setData(Shorts.toByteArray((short) value));
}

From source file:com.ebay.pulsar.analytics.metricstore.druid.postaggregator.ConstantPostAggregator.java

@Override
public byte[] cacheKey() {
    // Depending on the Number subclass type
    if (this.value instanceof Long) {
        ByteBuffer buffer = ByteBuffer.allocate(1 + 8).put(PostAggregatorCacheHelper.CONSTANT_CACHE_ID)
                .put(Longs.toByteArray((Long) value));
        return buffer.array();
    } else if (this.value instanceof Integer) {
        ByteBuffer buffer = ByteBuffer.allocate(1 + 4).put(PostAggregatorCacheHelper.CONSTANT_CACHE_ID)
                .put(Ints.toByteArray((Integer) value));
        return buffer.array();
    } else if (this.value instanceof Short) {
        ByteBuffer buffer = ByteBuffer.allocate(1 + 2).put(PostAggregatorCacheHelper.CONSTANT_CACHE_ID)
                .put(Shorts.toByteArray((Short) value));
        return buffer.array();
    } else if (this.value instanceof Byte) {
        ByteBuffer buffer = ByteBuffer.allocate(1 + 1).put(PostAggregatorCacheHelper.CONSTANT_CACHE_ID)
                .put((Byte) value);
        return buffer.array();
    } else if (this.value instanceof Double) {
        ByteBuffer buffer = ByteBuffer.allocate(1 + 8).put(PostAggregatorCacheHelper.CONSTANT_CACHE_ID)
                .putDouble((Double) value);
        return buffer.array();
    } else if (this.value instanceof Float) {
        ByteBuffer buffer = ByteBuffer.allocate(1 + 4).put(PostAggregatorCacheHelper.CONSTANT_CACHE_ID)
                .putFloat((Float) value);
        return buffer.array();
    } else if (this.value instanceof BigDecimal) {
        Double bigDouble = this.value.doubleValue();
        ByteBuffer buffer = ByteBuffer.allocate(1 + 8).put(PostAggregatorCacheHelper.CONSTANT_CACHE_ID)
                .putDouble((Double) bigDouble);
        return buffer.array();
    } else if (this.value instanceof BigInteger) {
        byte[] bigIntBytes = ((BigInteger) this.value).toByteArray();
        int len = bigIntBytes.length;
        ByteBuffer buffer = ByteBuffer.allocate(1 + len).put(PostAggregatorCacheHelper.CONSTANT_CACHE_ID)
                .put(bigIntBytes);//www  .jav  a  2  s  .com
        return buffer.array();
    } else if (this.value instanceof AtomicInteger) {
        int intVal = this.value.intValue();
        ByteBuffer buffer = ByteBuffer.allocate(1 + 4).put(PostAggregatorCacheHelper.CONSTANT_CACHE_ID)
                .put(Ints.toByteArray(intVal));
        return buffer.array();
    } else {
        long longVal = this.value.longValue();
        ByteBuffer buffer = ByteBuffer.allocate(1 + 8).put(PostAggregatorCacheHelper.CONSTANT_CACHE_ID)
                .put(Longs.toByteArray(longVal));
        return buffer.array();
    }

}

From source file:org.onosproject.ipfix.packet.InformationElement.java

public byte[] getBytes() throws HeaderException {
    try {//from   ww  w . ja v a  2 s.  co m
        byte[] data = new byte[LENGTH];
        // information element ID
        System.arraycopy(Shorts.toByteArray((short) getInformationElementID()), 0, data, 0, 2);
        // field length
        System.arraycopy(Shorts.toByteArray((short) getFieldLength()), 0, data, 2, 2);
        return data;
    } catch (Exception e) {
        throw new HeaderException("Error while generating the bytes: " + e.getMessage());
    }
}

From source file:org.onosproject.ipfix.packet.TemplateRecord.java

public byte[] getBytes() throws HeaderException {
    try {//from www  . j a  v  a 2 s. c  o  m
        int length = HEADER_LENGTH + (informationElements.size() * InformationElement.LENGTH);
        byte[] data = new byte[length];
        // template ID
        System.arraycopy(Shorts.toByteArray((short) getTemplateID()), 0, data, 0, 2);
        // field count
        System.arraycopy(Shorts.toByteArray((short) getFieldCount()), 0, data, 2, 2);
        // information elements
        int offset = HEADER_LENGTH;
        for (InformationElement ie : informationElements) {
            System.arraycopy(ie.getBytes(), 0, data, offset, InformationElement.LENGTH);
            offset += InformationElement.LENGTH;
        }
        return data;
    } catch (Exception e) {
        throw new HeaderException("Error while generating the bytes: " + e.getMessage());
    }
}

From source file:org.onosproject.ipfix.DataRecordRfwdMac.java

@Override
public byte[] getBytes() throws HeaderException {
    try {/*from w  w  w .  j av  a2 s  . c  o m*/
        byte[] data = new byte[LENGTH];

        System.arraycopy(exporterIPv4Address.toOctets(), 0, data, 0, 4);
        System.arraycopy(exporterIPv6Address.toOctets(), 0, data, 4, 16);
        System.arraycopy(Longs.toByteArray(flowStartMilliseconds), 0, data, 20, 8);
        System.arraycopy(Longs.toByteArray(flowEndMilliseconds), 0, data, 28, 8);
        System.arraycopy(Longs.toByteArray(octetDeltaCount), 0, data, 36, 8);
        System.arraycopy(Longs.toByteArray(packetDeltaCount), 0, data, 44, 8);
        System.arraycopy(Ints.toByteArray(ingressInterface), 0, data, 52, 4);
        System.arraycopy(Ints.toByteArray(egressInterface), 0, data, 56, 4);
        System.arraycopy(sourceMacAddress.toBytes(), 0, data, 60, 6);
        System.arraycopy(destinationMacAddress.toBytes(), 0, data, 66, 6);
        System.arraycopy(Shorts.toByteArray(ethernetType), 0, data, 72, 2);
        System.arraycopy(Shorts.toByteArray(vlanId), 0, data, 74, 2);

        return data;
    } catch (Exception e) {
        throw new HeaderException("Error while generating the bytes: " + e.getMessage());
    }
}

From source file:org.onosproject.ipfix.packet.OptionTemplateRecord.java

public byte[] getBytes() throws HeaderException {
    try {/*from  ww w . ja va  2s .c  om*/
        int length = HEADER_LENGTH + (scopeInformationElements.size() * InformationElement.LENGTH)
                + (informationElements.size() * InformationElement.LENGTH);
        if (length % 4 != 0) {
            length += (length % 4); // padding
        }
        byte[] data = new byte[length];
        // template ID
        System.arraycopy(Shorts.toByteArray((short) getTemplateID()), 0, data, 0, 2);
        // field count
        System.arraycopy(Shorts.toByteArray((short) getFieldCount()), 0, data, 2, 2);
        // scope field count
        System.arraycopy(Shorts.toByteArray((short) getScopeFieldCount()), 0, data, 4, 2);
        // information elements
        int offset = HEADER_LENGTH;
        for (InformationElement ie : scopeInformationElements) {
            System.arraycopy(ie.getBytes(), 0, data, offset, InformationElement.LENGTH);
            offset += InformationElement.LENGTH;
        }
        for (InformationElement ie : informationElements) {
            System.arraycopy(ie.getBytes(), 0, data, offset, InformationElement.LENGTH);
            offset += InformationElement.LENGTH;
        }
        return data;
    } catch (Exception e) {
        throw new HeaderException("Error while generating the bytes: " + e.getMessage());
    }
}