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:Main.java

static public int[] getArray(ByteBuffer data, int dataSize) {
    int size = data.getInt();
    int[] result = new int[size];
    for (int i = 0; i < result.length; i++) {
        if (dataSize == 2)
            result[i] = data.getShort();
        if (dataSize == 4)
            result[i] = data.getInt();/*  w w w  .  j a  v  a 2 s. c  om*/
    }
    return result;
}

From source file:org.bimserver.utils.BinUtils.java

public static Short byteArrayToShort(byte[] data) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(data);
    return byteBuffer.getShort();
}

From source file:com.sm.store.Utils.java

public static Value bytesToValue(byte[] bytes) {
    int len = bytes.length;
    ByteBuffer buf = ByteBuffer.wrap(bytes);
    long ver = buf.getLong();
    short node = buf.getShort();
    byte[] data = new byte[len - 10];
    buf.get(data);//ww w . j a v a  2s .c o m
    return CacheValue.createValue(data, ver, node);
}

From source file:com.jadarstudios.rankcapes.bukkit.CapePackValidator.java

/**
 * Utility method to detect if the bytes given are a zip file.
 *
 * @param bytes the bytes of the file/*from ww  w. j av  a  2  s  . c  om*/
 *
 * @return if it is a zip file
 */
public static boolean isZipFile(byte[] bytes) {
    ByteArrayInputStream input = new ByteArrayInputStream(bytes);

    ByteBuffer buffer = ByteBuffer.allocate(4);
    input.read(buffer.array(), 0, buffer.capacity());

    short packIdentifier = buffer.getShort();

    return packIdentifier == ZIP_IDENTIFIER;
}

From source file:com.nridge.core.base.std.BufUtl.java

/**
 * Retrieves a <i>boolean</i> value stored within the body of the
 * <code>ByteBuffer</code> object.
 *
 * @param aBuffer Packet byte buffer object.
 * @return <i>true</i> or <i>false</i> based on the value stored within
 * the byte buffer object./*from   www  .jav  a2  s .  c om*/
 */
public static boolean getBool(ByteBuffer aBuffer) {
    short flagValue;

    flagValue = aBuffer.getShort();
    return (flagValue == 1);
}

From source file:Main.java

/**
 * @param columnarKeyBlockData// w  w  w  .  ja v  a2s.  c  o m
 * @param columnarKeyStoreMetadata
 * @return
 * @author s71955 The high cardinality dimensions rows will be send in byte
 * array with its data length appended in the
 * ColumnarKeyStoreDataHolder byte array since high cardinality dim
 * data will not be part of MDKey/Surrogate keys. In this method the
 * byte array will be scanned and the length which is stored in
 * short will be removed.
 */
public static List<byte[]> readColumnarKeyBlockDataForNoDictionaryCols(byte[] columnarKeyBlockData) {
    List<byte[]> columnarKeyBlockDataList = new ArrayList<byte[]>(50);
    ByteBuffer noDictionaryValKeyStoreDataHolder = ByteBuffer.allocate(columnarKeyBlockData.length);
    noDictionaryValKeyStoreDataHolder.put(columnarKeyBlockData);
    noDictionaryValKeyStoreDataHolder.flip();
    while (noDictionaryValKeyStoreDataHolder.hasRemaining()) {
        short dataLength = noDictionaryValKeyStoreDataHolder.getShort();
        byte[] noDictionaryValKeyData = new byte[dataLength];
        noDictionaryValKeyStoreDataHolder.get(noDictionaryValKeyData);
        columnarKeyBlockDataList.add(noDictionaryValKeyData);
    }
    return columnarKeyBlockDataList;

}

From source file:ConversionUtil.java

public static short convertToShort(byte[] array) {
    ByteBuffer buffer = ByteBuffer.wrap(array);
    return buffer.getShort();
    /*/*from  ww  w. ja v a2s .  co m*/
    short value = 0;
    for (int i =0;i<array.length; ++i) {
    int offset = (array.length -i-1) *8;
    value += (array[i] << offset);
    // bytes[i] = (byte)((value & (0xff << offset)) >>> offset);
    }
    return value;
     */
}

From source file:experts.net.ip6.ULUA.java

/**
 * Convert ByteBuffer to Short List//from   ww w. j  a  va  2s.c  o m
 *
 * @param buf
 *            ByteBuffer
 * @return Short List
 */
private static List<Short> toList(ByteBuffer buf) {
    int cap = buf.capacity() / 2;
    List<Short> tmp = new ArrayList<>(cap);

    buf.rewind();
    for (int i = 0; i < cap; i++) {
        tmp.add(buf.getShort());
    } // for

    return tmp;
}

From source file:org.zuinnote.hadoop.bitcoin.format.BitcoinUtil.java

/**
* Converts a variable length integer (https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer) to long
*
* @param varInt byte array containing variable length integer
* 
* @return long corresponding to variable length integer
*
*///www  .  ja  va  2  s  .  c o m

public static long getVarInt(byte[] varInt) {
    long result = 0;
    if (varInt.length == 0)
        return result;
    int unsignedByte = varInt[0] & 0xFF;
    if (unsignedByte < 0xFD)
        return unsignedByte;
    int intSize = 0;
    if (unsignedByte == 0xFD)
        intSize = 3;
    else if (unsignedByte == 0xFE)
        intSize = 5;
    else if (unsignedByte == 0XFF)
        intSize = 9;
    byte[] rawDataInt = reverseByteArray(Arrays.copyOfRange(varInt, 1, intSize));
    ByteBuffer byteBuffer = ByteBuffer.wrap(rawDataInt);
    if (intSize == 3)
        result = byteBuffer.getShort();
    else if (intSize == 5)
        result = byteBuffer.getInt();
    else if (intSize == 9)
        result = byteBuffer.getLong(); // Need to handle sign - available only in JDK8
    return result;
}

From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServices.java

static Pair<byte[], Long> decompose(ByteBuffer composite) {
    composite = composite.slice().order(ByteOrder.BIG_ENDIAN);

    short len = composite.getShort();
    byte[] colName = new byte[len];
    composite.get(colName);//from  w  w w .  j  ava2s  .c  o  m

    short shouldBeZero = composite.getShort();
    Validate.isTrue(shouldBeZero == 0);

    byte shouldBe8 = composite.get();
    Validate.isTrue(shouldBe8 == 8);
    long ts = composite.getLong();

    return Pair.create(colName, (~ts));
}