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:au.org.ala.delta.io.BinFile.java

public short readShort() {
    ByteBuffer b = readByteBuffer(2);
    _stats.ReadShort++;
    return b.getShort();
}

From source file:org.cosmo.common.util.Util.java

public static short[] longToShorts2(long value) {
    short[] shorts = new short[4];
    ByteBuffer b = ByteBuffer.allocate(8);
    b.putLong(value);//  w w  w .ja v a  2 s  .  c  om

    b.rewind();
    shorts[0] = b.getShort();
    shorts[1] = b.getShort();
    shorts[2] = b.getShort();
    shorts[3] = b.getShort();
    return shorts;

}

From source file:com.srotya.monitoring.kafka.util.KafkaConsumerOffsetUtil.java

private long readOffsetMessageValue(ByteBuffer buffer) {
    buffer.getShort(); // read and ignore version
    long offset = buffer.getLong();
    return offset;
}

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

/**
 * Constructs a {@link MapPcpResponse} 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 there's not enough or too much data remaining in the buffer, or if the version doesn't match the
 * expected version (must always be {@code 2}), or if the r-flag isn't set, or if there's an unsuccessful/unrecognized result code,
 * or if the op code doesn't match the MAP opcode, or if the response has a {@code 0} for its {@code internalPort} or
 * {@code assignedExternalPort} field, or if there were problems parsing options
 */// w  w w . jav a2 s  .  co m
public MapPcpResponse(ByteBuffer buffer) {
    super(buffer);

    Validate.isTrue(super.getOp() == 1);

    mappingNonce = ByteBuffer.allocate(12);
    buffer.get(mappingNonce.array());
    mappingNonce = mappingNonce.asReadOnlyBuffer();
    this.protocol = buffer.get() & 0xFF;

    for (int i = 0; i < 3; i++) { // reserved block
        buffer.get();
    }

    this.internalPort = buffer.getShort() & 0xFFFF;
    this.assignedExternalPort = buffer.getShort() & 0xFFFF;
    byte[] addrArr = new byte[16];
    buffer.get(addrArr);
    try {
        this.assignedExternalIpAddress = InetAddress.getByAddress(addrArr); // should automatically shift down to ipv4 if ipv4-to-ipv6
                                                                            // mapped address
    } catch (UnknownHostException uhe) {
        throw new IllegalArgumentException(uhe); // should never happen, will always be 16 bytes
    }

    Validate.inclusiveBetween(0, 255, protocol); // should never happen
    Validate.inclusiveBetween(0, 65535, internalPort); // can be 0 if referencing all
    Validate.inclusiveBetween(0, 65535, assignedExternalPort); // can be 0 if removing

    parseOptions(buffer);
}

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

/**
 * Constructs a {@link PeerPcpResponse} 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 there's not enough or too much data remaining in the buffer, or if the version doesn't match the
 * expected version (must always be {@code 2}), or if the r-flag isn't set, or if there's an unsuccessful/unrecognized result code,
 * or if the op code doesn't match the PEER opcode, or if the response has a {@code 0} for its {@code internalPort} or
 * {@code assignedExternalPort} or {@code remotePeerPort} or {@code protocol} field, or if there were problems parsing options
 *//*from w w w  . j a  v a 2 s  .  c o m*/
public PeerPcpResponse(ByteBuffer buffer) {
    super(buffer);

    Validate.isTrue(super.getOp() == 2);

    mappingNonce = ByteBuffer.allocate(12);
    buffer.get(mappingNonce.array());
    mappingNonce = mappingNonce.asReadOnlyBuffer();
    this.protocol = buffer.get() & 0xFF;

    for (int i = 0; i < 3; i++) { // reserved block
        buffer.get();
    }

    this.internalPort = buffer.getShort() & 0xFFFF;
    this.assignedExternalPort = buffer.getShort() & 0xFFFF;
    byte[] addrArr = new byte[16];
    buffer.get(addrArr);
    try {
        this.assignedExternalIpAddress = InetAddress.getByAddress(addrArr); // should automatically shift down to ipv4 if ipv4-to-ipv6
                                                                            // mapped address
    } catch (UnknownHostException uhe) {
        throw new IllegalArgumentException(uhe); // should never happen, will always be 16 bytes
    }
    this.remotePeerPort = buffer.getShort() & 0xFFFF;

    for (int i = 0; i < 2; i++) { // reserved block
        buffer.get();
    }

    buffer.get(addrArr);
    try {
        this.remotePeerIpAddress = InetAddress.getByAddress(addrArr); // should automatically shift down to ipv4 if ipv4-to-ipv6
                                                                      // mapped address
    } catch (UnknownHostException uhe) {
        throw new IllegalArgumentException(uhe); // should never happen, will always be 16 bytes
    }

    Validate.inclusiveBetween(1, 255, protocol);
    Validate.inclusiveBetween(1, 65535, internalPort);
    Validate.inclusiveBetween(1, 65535, assignedExternalPort);
    Validate.inclusiveBetween(1, 65535, remotePeerPort);

    parseOptions(buffer);
}

From source file:com.diozero.devices.ADXL345.java

@Override
public Vector3D getAccelerometerData() throws RuntimeIOException {
    ByteBuffer data = ByteBuffer.wrap(device.readBytes(AXES_DATA, 6));
    short x = data.getShort();
    short y = data.getShort();
    short z = data.getShort();

    return ImuDataFactory.createVector(new short[] { x, y, z }, SCALE_FACTOR);
}

From source file:org.jmangos.sniffer.network.model.impl.WOWNetworkChannel.java

@Override
public void onResiveClientData(final long frameNumber, final byte[] buffer) {
    final ByteBuffer bytebuf = ByteBuffer.wrap(buffer);
    long opcode = 0;
    long size = 0;
    byte[] data;/*from ww w. j  ava2 s  .  c  o  m*/
    bytebuf.order(ByteOrder.LITTLE_ENDIAN);
    if (getChannelState().contains(State.AUTHED)) {
        final long header = bytebuf.getInt() & 0xFFFFFFFF;
        size = header >> 13;
        opcode = (header & 0x1FFF);
        data = new byte[(int) size];
        bytebuf.get(data);
    } else {
        size = bytebuf.getShort();
        opcode = bytebuf.getInt();
        data = new byte[(int) size - 4];
        bytebuf.get(data);
        // old (opcode == 0x1a72) | (opcode == 0x3f3)
        if ((opcode == 0x1aa1) | (opcode == 0x9f1)) {
            this.log.debug("Init cryptography system for channel {}", this.getChannelId());
            addChannelState(State.AUTHED);
            this.csPacketBuffer.getCrypt().init(this.woWKeyReader.getKey());
            this.scPacketBuffer.getCrypt().init(this.woWKeyReader.getKey());
        }
    }
    this.log.debug(String.format("Frame: %d; Resive packet %s Opcode: 0x%x\n", frameNumber, "CMSG", opcode));
    for (final PacketLogHandler logger : this.packetLoggers) {
        logger.onDecodePacket(this, Direction.CLIENT, (int) size, (int) opcode, data, (int) frameNumber);
    }
}

From source file:org.jmangos.sniffer.network.model.impl.WOWNetworkChannel.java

@Override
synchronized public void onResiveServerData(final long frameNumber, final byte[] buffer) {
    final ByteBuffer bytebuf = ByteBuffer.wrap(buffer);
    bytebuf.order(ByteOrder.LITTLE_ENDIAN);
    long opcode = 0;
    long size = 0;
    byte[] data = null;
    if (getChannelState().contains(State.AUTHED)) {
        final long header = bytebuf.getInt() & 0xFFFFFFFF;
        size = header >> 13;//from  ww w  .  ja va2s .  c  om
        opcode = header & 0x1FFF;
        data = new byte[(int) size];
        bytebuf.get(data);
    } else {
        size = bytebuf.getShort();
        opcode = bytebuf.getInt();
        bytebuf.mark();
        data = new byte[(int) size - 4];
        bytebuf.get(data);
        bytebuf.reset();
        // old 0xc0b
        if ((opcode == 0x221) & !getChannelState().contains(State.NOT_ACCEPT_SEED)) {
            this.log.debug("Get new Seed");
            final byte[] serverSeed = new byte[16];
            final byte[] clientSeed = new byte[16];
            for (int i = 0; i < 16; i++) {
                serverSeed[i] = bytebuf.get();
            }
            for (int i = 0; i < 16; i++) {
                clientSeed[i] = bytebuf.get();
            }
            bytebuf.get();
            this.csPacketBuffer.getCrypt().setEncryptionSeed(clientSeed);
            this.scPacketBuffer.getCrypt().setEncryptionSeed(serverSeed);
        }

    }
    this.log.debug(String.format("Frame: %d; Resive packet %s Opcode: 0x%x OpcodeSize: %d", frameNumber, "SMSG",
            opcode, size));
    for (final PacketLogHandler logger : this.packetLoggers) {
        logger.onDecodePacket(this, Direction.SERVER, (int) size, (int) opcode, data, (int) frameNumber);
    }
}

From source file:org.apache.carbondata.processing.store.writer.AbstractFactDataWriter.java

/**
 * Below method will be used to update the min or max value
 * by removing the length from it//from w  ww .j av  a2 s .  c o m
 *
 * @return min max value without length
 */
protected byte[] updateMinMaxForNoDictionary(byte[] valueWithLength) {
    ByteBuffer buffer = ByteBuffer.wrap(valueWithLength);
    byte[] actualValue = new byte[buffer.getShort()];
    buffer.get(actualValue);
    return actualValue;
}