Example usage for org.apache.commons.io EndianUtils readSwappedShort

List of usage examples for org.apache.commons.io EndianUtils readSwappedShort

Introduction

In this page you can find the example usage for org.apache.commons.io EndianUtils readSwappedShort.

Prototype

public static short readSwappedShort(byte[] data, int offset) 

Source Link

Document

Reads a "short" value from a byte array at a given offset.

Usage

From source file:io.github.dsheirer.source.tuner.hackrf.HackRFTunerController.java

public int read(Request request, int value, int index, int length) throws UsbException {
    if (!(length == 1 || length == 2 || length == 4)) {
        throw new IllegalArgumentException(
                "invalid length [" + length + "] must be: byte=1, short=2, int=4 to read a primitive");
    }/* www.  j a  v  a2  s .c  om*/

    ByteBuffer buffer = readArray(request, value, index, length);

    byte[] data = new byte[buffer.capacity()];

    buffer.get(data);

    switch (data.length) {
    case 1:
        return data[0];
    case 2:
        return EndianUtils.readSwappedShort(data, 0);
    case 4:
        return EndianUtils.readSwappedInteger(data, 0);
    default:
        throw new UsbException(
                "read() primitive returned an " + "unrecognized byte array " + Arrays.toString(data));
    }
}