Example usage for java.nio ByteBuffer getShort

List of usage examples for java.nio ByteBuffer getShort

Introduction

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

Prototype

public abstract short getShort();

Source Link

Document

Returns the short at the current position and increases the position by 2.

Usage

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

/**
 * Constructs a {@link TcpMappingNatPmpResponse} object by parsing a buffer.
 * @param buffer buffer containing PCP response data
 * @throws NullPointerException if any argument is {@code null}
 * @throws BufferUnderflowException if not enough data is available in {@code buffer}
 * @throws IllegalArgumentException if the version doesn't match the expected version (must always be {@code 0}), or if the op
 * {@code != 129}, or if external port in the buffer is {@code < 1 || > 65535}, or if there's an unsuccessful/unrecognized result code
 *///  ww w .jav a2  s  . c  o m
public TcpMappingNatPmpResponse(ByteBuffer buffer) {
    super(buffer);

    Validate.isTrue(getOp() == 130);

    internalPort = buffer.getShort() & 0xFFFF;
    externalPort = buffer.getShort() & 0xFFFF;
    lifetime = buffer.getInt() & 0xFFFFFFFFL;

    Validate.inclusiveBetween(0, 65535, externalPort);
}

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

/**
 * Constructs a {@link UdpMappingNatPmpResponse} object by parsing a buffer.
 * @param buffer buffer containing PCP response data
 * @throws NullPointerException if any argument is {@code null}
 * @throws BufferUnderflowException if not enough data is available in {@code buffer}
 * @throws IllegalArgumentException if the version doesn't match the expected version (must always be {@code 0}), or if the op
 * {@code != 129}, or if external port in the buffer is {@code < 1 || > 65535}, or if there's an unsuccessful/unrecognized result code
 *///from   ww  w  . j  a  v a  2s.  c  om
public UdpMappingNatPmpResponse(ByteBuffer buffer) {
    super(buffer);

    Validate.isTrue(getOp() == 129);

    internalPort = buffer.getShort() & 0xFFFF;
    externalPort = buffer.getShort() & 0xFFFF;
    lifetime = buffer.getInt() & 0xFFFFFFFFL;

    Validate.inclusiveBetween(0, 65535, externalPort); // 0 valid for deletion
}

From source file:com.offbynull.portmapper.pcp.PcpOption.java

/**
 * Constructs a {@link PcpOption} object by parsing a buffer.
 * @param buffer buffer containing PCP option data
 * @throws NullPointerException if any argument is {@code null}
 * @throws BufferUnderflowException if not enough data is available in {@code buffer}
 *//*w  ww .  jav a 2 s.c  o m*/
public PcpOption(ByteBuffer buffer) {
    Validate.notNull(buffer);

    code = buffer.get() & 0xFF;

    buffer.get(); // skip over reserved

    length = buffer.getShort() & 0xFFFF;

    byte[] dataArr = new byte[length];
    buffer.get(dataArr);

    data = ByteBuffer.wrap(dataArr).asReadOnlyBuffer();

    // skip over padding
    int remainder = length % 4;
    for (int i = 0; i < remainder; i++) {
        buffer.get();
    }
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6StatsRequest.java

@Override
public void readFrom(ByteBuffer data) {
    this.vendor = data.getInt();
    this.msgsubtype = data.getInt();
    data.getInt();//pad 4 bytes
    this.outPort = data.getShort();
    this.match_len = data.getShort();
    this.tableId = data.get();
    for (int i = 0; i < 3; i++)
        data.get();//pad byte

}

From source file:ru.jts_dev.gameserver.movement.geoengine.GeoService.java

public void loadRegion(int x, int y, ByteBuffer byteBuffer) {
    Region region = new Region();
    int index = 0;

    byteBuffer.position(18);//from   w w  w .  jav  a 2s .co m
    while (byteBuffer.hasRemaining()) {
        short type = byteBuffer.getShort();

        if (type == 0x0000) {
            // 1x short, flat block
            short maxHeight = (short) (byteBuffer.getShort() & 0x0fff0);
            short minHeight = (short) (byteBuffer.getShort() & 0x0fff0);

            Block block = new FlatBlock(minHeight, maxHeight);
            region.addBlock(x, y, index, block);
        } else if (type == 0x0040) { //type id from rebellion
            // 64x short, complex block
            Cell[] cells = new Cell[64];
            for (int cell = 0; cell < 64; cell++) {
                int value = byteBuffer.getShort();

                short height = (short) ((short) (value & 0x0fff0) >> 1);
                byte direction = (byte) (value & 0x0F);
                cells[cell] = new Cell(direction, height);
            }

            Block block = new ComplexBlock(cells);
            region.addBlock(x, y, index, block);
        } else { //0x0048 block id
            // 64x-8192x short, multilevel block
            short[][] height = new short[64][];
            byte[][] NSWE = new byte[64][];
            byte[] layersa = new byte[64];
            for (int cell = 0; cell < 64; cell++) {
                short layers = byteBuffer.getShort();

                height[cell] = new short[layers];
                NSWE[cell] = new byte[layers];
                for (int i = 0; i < layers; i++) {
                    int value = byteBuffer.getShort();
                    height[cell][i] = (short) ((short) (value & 0x0fff0) >> 1);
                    NSWE[cell][i] = (byte) (value & 0x0F);
                }
                layersa[cell] = (byte) --layers;
            }

            Block block = new MultilevelBlock(layersa, height, NSWE);
            region.addBlock(x, y, index, block);
        }

        index++;
    }
}

From source file:org.opendaylight.lispflowmapping.lisp.serializer.address.ExplicitLocatorPathSerializer.java

private Address deserializeData(ByteBuffer buffer, short lcafLength, LispAddressSerializerContext ctx) {
    List<Hop> hops = new ArrayList<Hop>();
    short length = lcafLength;
    while (length > 0) {
        byte flags = (byte) buffer.getShort();
        boolean lookup = ByteUtil.extractBit(flags, Flags.LOOKUP);
        boolean rlocProbe = ByteUtil.extractBit(flags, Flags.RLOC_PROBE);
        boolean strict = ByteUtil.extractBit(flags, Flags.STRICT);
        SimpleAddress address = SimpleAddressSerializer.getInstance().deserialize(buffer, ctx);
        HopBuilder builder = new HopBuilder();
        builder.setLrsBits(new LrsBits(lookup, rlocProbe, strict));
        builder.setAddress(address);//from  w ww. ja v a 2  s .c  o  m
        builder.setHopId("Hop " + (hops.size() + 1));
        length -= SimpleAddressSerializer.getInstance().getAddressSize(address) + 2;
        hops.add(builder.build());
    }
    return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPathBuilder()
            .setExplicitLocatorPath(new ExplicitLocatorPathBuilder().setHop(hops).build()).build();
}

From source file:nl.dobots.bluenet.ble.base.structs.CrownstoneServiceData.java

public CrownstoneServiceData(byte[] bytes) {
    super();/*from   ww  w  .j  a v a2 s  .com*/

    ByteBuffer bb = ByteBuffer.wrap(bytes);
    bb.order(ByteOrder.LITTLE_ENDIAN);

    bb.getShort(); // skip first two bytes (service UUID)

    bb.mark();
    int val = bb.get();

    if (val == 1) {
        setCrownstoneId(bb.getShort());
        setCrownstoneStateId(bb.getShort());
        setSwitchState((bb.get() & 0xff));
        setEventBitmask(bb.get());
        setTemperature(bb.get());
        setPowerUsage(bb.getInt());
        setAccumulatedEnergy(bb.getInt());

        setRelayState(BleUtils.isBitSet(getSwitchState(), 7));
        setPwm(getSwitchState() & ~(1 << 7));
    } else {
        bb.reset();
        setCrownstoneId(bb.getShort());
        setCrownstoneStateId(bb.getShort());
        setSwitchState((bb.get() & 0xff));
        setEventBitmask(bb.get());
        setTemperature(bb.get());
        bb.get(); // skip reserved
        //         bb.getShort(); // skip reserved
        setPowerUsage(bb.getInt());
        setAccumulatedEnergy(bb.getInt());

        setRelayState(BleUtils.isBitSet(getSwitchState(), 7));
        setPwm(getSwitchState() & ~(1 << 7));
    }
}

From source file:com.offbynull.portmapper.pcp.FilterPcpOption.java

/**
 * Constructs a {@link FilterPcpOption} by parsing a buffer.
 * @param buffer buffer containing PCP option data
 * @throws NullPointerException if any argument is {@code null}
 * @throws BufferUnderflowException if not enough data is available in {@code buffer}
 * @throws IllegalArgumentException if option code is not {@code 3}, or if the field {@code prefixLength > 128}
 */// w  w w .j  av  a  2  s. com
public FilterPcpOption(ByteBuffer buffer) {
    super(buffer);

    Validate.isTrue(super.getCode() == 3);

    buffer.get(); // reserved
    prefixLength = buffer.get() & 0xFF;
    remotePeerPort = buffer.getShort() & 0xFFFF;

    Validate.inclusiveBetween(0, 128, prefixLength); // 0 indicates 'no filter'
    Validate.inclusiveBetween(0, 65535, remotePeerPort); // 0 indicates 'all ports', should never trigger

    byte[] addrArr = new byte[16];
    buffer.get(addrArr);
    try {
        remotePeerIpAddress = InetAddress.getByAddress(addrArr);
    } catch (UnknownHostException uhe) {
        throw new IllegalStateException(uhe); // should never happen
    }
}

From source file:io.github.dsheirer.sample.adapter.ChannelShortAdapter.java

@Override
public float[] convert(byte[] samples) {
    float[] processed = new float[samples.length / 4];

    int pointer = 0;

    /* Wrap byte array in a byte buffer so we can process them as shorts */
    ByteBuffer buffer = ByteBuffer.wrap(samples);

    /* Set endian to correct byte ordering */
    buffer.order(mByteOrder);//from   w  ww .j  av a  2  s. c  o  m

    while (buffer.hasRemaining()) {
        if (mMixerChannel == MixerChannel.LEFT) {
            processed[pointer] = mMap.get(buffer.getShort());

            /* Throw away the right channel */
            buffer.getShort();
        } else {
            /* Throw away the left channel */
            buffer.getShort();

            processed[pointer] = mMap.get(buffer.getShort());
        }

        pointer++;
    }

    return processed;
}

From source file:com.netflix.aegisthus.pig.AegisthusLoadCaster.java

private long getNumber(byte[] arg0) throws Exception {
    byte[] by = hex.decode(arg0);
    ByteBuffer bb = ByteBuffer.allocate(by.length);
    bb.put(by);//  w w w.  j  a  va 2  s. com
    bb.position(0);
    switch (by.length) {
    case 1:
        return (long) bb.get();
    case 2:
        return (long) bb.getShort();
    case 4:
        return (long) bb.getInt();
    case 8:
        return (long) bb.getLong();
    }
    throw new UnexpectedException("couldn't determine datatype");
}