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

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);

    bb.asShortBuffer().put((short) 471142);
    bb.rewind();//from   w w  w .ja va 2s .c  o m
    System.out.println(bb.getShort());

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer buf = ByteBuffer.allocate(100);

    buf.putShort((short) 123);

    // Reset position for reading
    buf.flip();/*from  w  w w.j  a  v  a 2s.  c o m*/

    // Retrieve the values
    short s = buf.getShort();

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asCharBuffer().put("Howdy!");
    char c;/*ww w .  ja  va2  s.  c o m*/
    while ((c = bb.getChar()) != 0)
        System.out.print(c + " ");
    System.out.println();

    bb.rewind();
    // Store and read a short:
    bb.asShortBuffer().put((short) 471142);
    System.out.println(bb.getShort());

}

From source file:GetData.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    // Allocation automatically zeroes the ByteBuffer:
    int i = 0;//from w  w  w .  ja  v a2  s  .  c o  m
    while (i++ < bb.limit())
        if (bb.get() != 0)
            System.out.println("nonzero");
    System.out.println("i = " + i);
    bb.rewind();
    // Store and read a char array:
    bb.asCharBuffer().put("Howdy!");
    char c;
    while ((c = bb.getChar()) != 0)
        System.out.print(c + " ");
    System.out.println();
    bb.rewind();
    // Store and read a short:
    bb.asShortBuffer().put((short) 471142);
    System.out.println(bb.getShort());
    bb.rewind();
    // Store and read an int:
    bb.asIntBuffer().put(99471142);
    System.out.println(bb.getInt());
    bb.rewind();
    // Store and read a long:
    bb.asLongBuffer().put(99471142);
    System.out.println(bb.getLong());
    bb.rewind();
    // Store and read a float:
    bb.asFloatBuffer().put(99471142);
    System.out.println(bb.getFloat());
    bb.rewind();
    // Store and read a double:
    bb.asDoubleBuffer().put(99471142);
    System.out.println(bb.getDouble());
    bb.rewind();

}

From source file:Main.java

public static int getUnsignedShort(ByteBuffer bb) {
    return (bb.getShort() & 0xffff);
}

From source file:Main.java

public static int getUnsignedShort(final ByteBuffer pByteBuffer) {
    return pByteBuffer.getShort() & 0xFFFF;
}

From source file:Main.java

public static Short byteArrayToShort(byte[] bytes) {
    if (bytes.length != (Short.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to a short");
    }//from  w w w  . j a va 2s  . co m
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.BIG_ENDIAN);
    return buffer.getShort();
}

From source file:Main.java

public static Short byteArrayToShortLE(byte[] bytes) {
    if (bytes.length != (Short.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to a short");
    }/*from   w  ww.  ja  v  a 2  s. com*/
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getShort();
}

From source file:Main.java

public static byte[] downSample(final byte[] data) {
    final byte[] output = new byte[data.length / 2];
    int length = data.length;
    ByteBuffer bb = ByteBuffer.wrap(data);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    for (int i = 0; i < length; i += 2) {
        output[i / 2] = (byte) (((bb.getShort() * 128.0) / 32768.0) + 128.0);
    }/* w w w  . ja va  2  s. com*/
    return output;
}

From source file:Main.java

static public String getString(ByteBuffer data) {
    int size = data.get();
    if (size == 0) {
        return "";
    } else {//from w ww .jav  a2  s.co  m
        byte[] bb = new byte[size * 2 - 2];
        data.get(bb);
        data.getShort();
        try {
            return new String(bb, "UTF-16LE");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }
}