Search in sources :

Example 46 with ShortBuffer

use of java.nio.ShortBuffer in project robovm by robovm.

the class Main method storeValues.

static void storeValues(ByteBuffer directBuf) {
    directBuf.position(0);
    ShortBuffer shortBuf = directBuf.asShortBuffer();
    CharBuffer charBuf = directBuf.asCharBuffer();
    IntBuffer intBuf = directBuf.asIntBuffer();
    FloatBuffer floatBuf = directBuf.asFloatBuffer();
    LongBuffer longBuf = directBuf.asLongBuffer();
    DoubleBuffer doubleBuf = directBuf.asDoubleBuffer();
    final byte byteValue = -5;
    final short shortValue = -1234;
    final char charValue = 49200;
    final int intValue = 0x12345678;
    final float floatValue = 3.14159f;
    final long longValue = 0x1122334455667788L;
    final double doubleValue = Double.MIN_VALUE;
    if (directBuf.put(1, byteValue).get(1) != byteValue) {
        throw new RuntimeException("byte get/store failed");
    }
    if (shortBuf.put(1, shortValue).get(1) != shortValue) {
        throw new RuntimeException("short get/store failed");
    }
    if (charBuf.put(2, charValue).get(2) != charValue) {
        throw new RuntimeException("char get/store failed");
    }
    if (intBuf.put(2, intValue).get(2) != intValue) {
        throw new RuntimeException("int get/store failed");
    }
    if (floatBuf.put(3, floatValue).get(3) != floatValue) {
        throw new RuntimeException("float get/store failed");
    }
    if (longBuf.put(2, longValue).get(2) != longValue) {
        throw new RuntimeException("long get/store failed");
    }
    if (doubleBuf.put(3, doubleValue).get(3) != doubleValue) {
        throw new RuntimeException("double get/store failed");
    }
    directBuf.position(0);
    char[] outBuf = new char[directBuf.limit() * 2];
    for (int i = 0; i < directBuf.limit(); i++) {
        byte b = directBuf.get();
        outBuf[i * 2] = hexChar((byte) ((b >> 4) & 0x0f));
        outBuf[i * 2 + 1] = hexChar((byte) (b & 0x0f));
    }
    System.out.println(new String(outBuf));
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) LongBuffer(java.nio.LongBuffer) CharBuffer(java.nio.CharBuffer) FloatBuffer(java.nio.FloatBuffer) IntBuffer(java.nio.IntBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 47 with ShortBuffer

use of java.nio.ShortBuffer in project robovm by robovm.

the class OldAndroidNIOTest method byteBufferTest.

private void byteBufferTest(ByteBuffer b) {
    checkBuffer(b);
    // Duplicate buffers revert to big-endian.
    b.order(ByteOrder.LITTLE_ENDIAN);
    ByteBuffer dupe = b.duplicate();
    assertEquals(ByteOrder.BIG_ENDIAN, dupe.order());
    b.order(ByteOrder.BIG_ENDIAN);
    // Bounds checks
    try {
        b.put(-1, (byte) 0);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        b.put(b.limit(), (byte) 0);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // IndexOutOfBoundsException: offset < 0
    try {
        byte[] data = new byte[8];
        b.position(0);
        b.put(data, -1, 2);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // IndexOutOfBoundsException: length > array.length - offset
    try {
        byte[] data = new byte[8];
        b.position(0);
        b.put(data, 1, 8);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // BufferOverflowException: length > remaining()
    try {
        byte[] data = new byte[8];
        b.position(b.limit() - 2);
        b.put(data, 0, 3);
        fail("expected exception not thrown");
    } catch (BufferOverflowException e) {
    // expected
    }
    // Fill buffer with bytes A0 A1 A2 A3 ...
    b.position(0);
    for (int i = 0; i < b.capacity(); i++) {
        b.put((byte) (0xA0 + i));
    }
    try {
        b.put((byte) 0xFF);
        fail("expected exception not thrown");
    } catch (BufferOverflowException e) {
    // expected
    }
    b.position(0);
    assertEquals((byte) 0xA7, b.get(7));
    try {
        b.get(12);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        b.get(-10);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    b.position(0);
    b.order(ByteOrder.LITTLE_ENDIAN);
    assertEquals((byte) 0xA0, b.get());
    assertEquals((byte) 0xA1, b.get());
    assertEquals((byte) 0xA2, b.get());
    assertEquals((byte) 0xA3, b.get());
    assertEquals((byte) 0xA4, b.get());
    assertEquals((byte) 0xA5, b.get());
    assertEquals((byte) 0xA6, b.get());
    assertEquals((byte) 0xA7, b.get());
    assertEquals((byte) 0xA8, b.get());
    assertEquals((byte) 0xA9, b.get());
    assertEquals((byte) 0xAA, b.get());
    assertEquals((byte) 0xAB, b.get());
    try {
        b.get();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.position(0);
    b.order(ByteOrder.BIG_ENDIAN);
    assertEquals((byte) 0xA0, b.get());
    assertEquals((byte) 0xA1, b.get());
    assertEquals((byte) 0xA2, b.get());
    assertEquals((byte) 0xA3, b.get());
    assertEquals((byte) 0xA4, b.get());
    assertEquals((byte) 0xA5, b.get());
    assertEquals((byte) 0xA6, b.get());
    assertEquals((byte) 0xA7, b.get());
    assertEquals((byte) 0xA8, b.get());
    assertEquals((byte) 0xA9, b.get());
    assertEquals((byte) 0xAA, b.get());
    assertEquals((byte) 0xAB, b.get());
    try {
        b.get();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.position(6);
    b.limit(10);
    assertEquals((byte) 0xA6, b.get());
    // Check sliced buffer
    b.position(6);
    ByteBuffer bb = b.slice();
    checkBuffer(bb);
    assertEquals(0, bb.position());
    assertEquals(4, bb.limit());
    assertEquals(4, bb.capacity());
    assertEquals((byte) 0xA6, bb.get());
    assertEquals((byte) 0xA7, bb.get());
    assertEquals((byte) 0xA8, bb.get());
    assertEquals((byte) 0xA9, bb.get());
    try {
        bb.get();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    // Reset position and limit
    b.position(0);
    b.limit(b.capacity());
    // Check 'getShort'
    b.order(ByteOrder.LITTLE_ENDIAN);
    b.position(0);
    assertEquals((short) 0xA1A0, b.getShort());
    assertEquals((short) 0xA3A2, b.getShort());
    assertEquals((short) 0xA5A4, b.getShort());
    assertEquals((short) 0xA7A6, b.getShort());
    assertEquals((short) 0xA9A8, b.getShort());
    assertEquals((short) 0xABAA, b.getShort());
    try {
        bb.getShort();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.order(ByteOrder.BIG_ENDIAN);
    b.position(0);
    assertEquals((short) 0xA0A1, b.getShort());
    assertEquals((short) 0xA2A3, b.getShort());
    assertEquals((short) 0xA4A5, b.getShort());
    assertEquals((short) 0xA6A7, b.getShort());
    assertEquals((short) 0xA8A9, b.getShort());
    assertEquals((short) 0xAAAB, b.getShort());
    try {
        bb.getShort();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    // Check 'getInt'
    b.order(ByteOrder.LITTLE_ENDIAN);
    b.position(0);
    assertEquals(0xA3A2A1A0, b.getInt());
    assertEquals(0xA7A6A5A4, b.getInt());
    assertEquals(0xABAAA9A8, b.getInt());
    try {
        bb.getInt();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.order(ByteOrder.BIG_ENDIAN);
    b.position(0);
    assertEquals(0xA0A1A2A3, b.getInt());
    assertEquals(0xA4A5A6A7, b.getInt());
    assertEquals(0xA8A9AAAB, b.getInt());
    try {
        bb.getInt();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    // Check 'getFloat'
    b.order(ByteOrder.LITTLE_ENDIAN);
    b.position(0);
    assertEquals(0xA3A2A1A0, Float.floatToRawIntBits(b.getFloat()));
    assertEquals(0xA7A6A5A4, Float.floatToRawIntBits(b.getFloat()));
    assertEquals(0xABAAA9A8, Float.floatToRawIntBits(b.getFloat()));
    try {
        b.getFloat();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.order(ByteOrder.BIG_ENDIAN);
    b.position(0);
    assertEquals(0xA0A1A2A3, Float.floatToRawIntBits(b.getFloat()));
    assertEquals(0xA4A5A6A7, Float.floatToRawIntBits(b.getFloat()));
    assertEquals(0xA8A9AAAB, Float.floatToRawIntBits(b.getFloat()));
    try {
        b.getFloat();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    // Check 'getDouble(int position)'
    b.order(ByteOrder.LITTLE_ENDIAN);
    assertEquals(0xA7A6A5A4A3A2A1A0L, Double.doubleToRawLongBits(b.getDouble(0)));
    assertEquals(0xA8A7A6A5A4A3A2A1L, Double.doubleToRawLongBits(b.getDouble(1)));
    try {
        b.getDouble(-1);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        b.getDouble(5);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    b.order(ByteOrder.BIG_ENDIAN);
    assertEquals(0xA0A1A2A3A4A5A6A7L, Double.doubleToRawLongBits(b.getDouble(0)));
    assertEquals(0xA1A2A3A4A5A6A7A8L, Double.doubleToRawLongBits(b.getDouble(1)));
    try {
        b.getDouble(-1);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        b.getDouble(5);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // Slice and check 'getInt'
    b.position(1);
    b.limit(5);
    b.order(ByteOrder.LITTLE_ENDIAN);
    bb = b.slice();
    assertEquals(4, bb.capacity());
    assertEquals(ByteOrder.BIG_ENDIAN, bb.order());
    assertEquals(0xA1A2A3A4, bb.getInt(0));
    bb.order(ByteOrder.LITTLE_ENDIAN);
    assertEquals(0xA4A3A2A1, bb.getInt(0));
    bb.order(ByteOrder.LITTLE_ENDIAN);
    ShortBuffer sb = bb.asShortBuffer();
    checkBuffer(sb);
    assertEquals(2, sb.capacity());
    assertEquals((short) 0xA2A1, sb.get());
    assertEquals((short) 0xA4A3, sb.get());
    bb.order(ByteOrder.BIG_ENDIAN);
    sb = bb.asShortBuffer();
    checkBuffer(sb);
    assertEquals(2, sb.capacity());
    assertEquals((short) 0xA1A2, sb.get());
    assertEquals((short) 0xA3A4, sb.get());
    bb.order(ByteOrder.LITTLE_ENDIAN);
    IntBuffer ib = bb.asIntBuffer();
    checkBuffer(ib);
    assertEquals(1, ib.capacity());
    assertEquals(0xA4A3A2A1, ib.get());
    bb.order(ByteOrder.BIG_ENDIAN);
    ib = bb.asIntBuffer();
    checkBuffer(ib);
    assertEquals(1, ib.capacity());
    assertEquals(0xA1A2A3A4, ib.get());
    bb.order(ByteOrder.LITTLE_ENDIAN);
    FloatBuffer fb = bb.asFloatBuffer();
    checkBuffer(fb);
    assertEquals(1, fb.capacity());
    assertEquals(0xA4A3A2A1, Float.floatToRawIntBits(fb.get()));
    bb.order(ByteOrder.BIG_ENDIAN);
    fb = bb.asFloatBuffer();
    checkBuffer(fb);
    assertEquals(1, fb.capacity());
    assertEquals(0xA1A2A3A4, Float.floatToRawIntBits(fb.get()));
}
Also used : IntBuffer(java.nio.IntBuffer) FloatBuffer(java.nio.FloatBuffer) BufferOverflowException(java.nio.BufferOverflowException) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 48 with ShortBuffer

use of java.nio.ShortBuffer in project robovm by robovm.

the class OldAndroidNIOTest method testNIO_short_array.

public void testNIO_short_array() throws Exception {
    // Test short array-based buffer
    short[] shortArray = new short[8];
    ShortBuffer sb = ShortBuffer.wrap(shortArray);
    shortBufferTest(sb);
}
Also used : ShortBuffer(java.nio.ShortBuffer)

Example 49 with ShortBuffer

use of java.nio.ShortBuffer in project robovm by robovm.

the class OldDirectShortBufferTest method testPutWhenOffsetIsNonZero.

/**
     * Regression for http://code.google.com/p/android/issues/detail?id=3279
     */
public void testPutWhenOffsetIsNonZero() {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(40);
    byteBuffer.order(ByteOrder.nativeOrder());
    ShortBuffer shortBuffer = byteBuffer.asShortBuffer();
    short[] source = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
    shortBuffer.put(source, 2, 2);
    shortBuffer.put(source, 4, 2);
    assertEquals(4, shortBuffer.get(0));
    assertEquals(5, shortBuffer.get(1));
    assertEquals(6, shortBuffer.get(2));
    assertEquals(7, shortBuffer.get(3));
}
Also used : ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 50 with ShortBuffer

use of java.nio.ShortBuffer in project BIMserver by opensourceBIM.

the class BinUtils method shortToByteArray.

public static byte[] shortToByteArray(short inShort) {
    byte[] bArray = new byte[2];
    ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
    ShortBuffer lBuffer = bBuffer.asShortBuffer();
    lBuffer.put(inShort);
    return bArray;
}
Also used : ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer)

Aggregations

ShortBuffer (java.nio.ShortBuffer)227 ByteBuffer (java.nio.ByteBuffer)78 FloatBuffer (java.nio.FloatBuffer)54 IntBuffer (java.nio.IntBuffer)45 DoubleBuffer (java.nio.DoubleBuffer)23 LongBuffer (java.nio.LongBuffer)16 Test (org.junit.Test)14 Buffer (java.nio.Buffer)11 BufferOverflowException (java.nio.BufferOverflowException)11 CharBuffer (java.nio.CharBuffer)11 VertexBuffer (com.jme3.scene.VertexBuffer)8 BufferUnderflowException (java.nio.BufferUnderflowException)7 BytePointer (org.bytedeco.javacpp.BytePointer)7 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)6 IOException (java.io.IOException)5 Vector3f (com.jme3.math.Vector3f)4 ArrayList (java.util.ArrayList)4 Bitmap (android.graphics.Bitmap)3 Mesh (com.jme3.scene.Mesh)3 InvalidMarkException (java.nio.InvalidMarkException)3