Search in sources :

Example 6 with LongBuffer

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

the class MachineSizedSIntPtr method set.

/**
     * Copies {@code count} ints from {@code src} starting at offset {@code offset}
     * to the memory pointed to by this {@link MachineSizedSIntPtr}. Does 
     * signed {@code int} to {@code long} conversion if running on a 64-bit 
     * platform.
     * 
     * @param src the source.
     * @param offset the offset within the source array to start copying from.
     * @param count the number of elements to copy.
     */
public void set(int[] src, int offset, int count) {
    if (_sizeOf() == 4) {
        asIntBuffer(count).put(src, offset, count);
    } else {
        Arrays.checkOffsetAndCount(src.length, offset, count);
        LongBuffer buf = asLongBuffer(count);
        for (int i = 0; i < count; i++) {
            buf.put(src[i + offset]);
        }
    }
}
Also used : LongBuffer(java.nio.LongBuffer)

Example 7 with LongBuffer

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

the class MachineSizedUIntPtr method set.

/**
     * Copies {@code count} ints from {@code src} starting at offset {@code offset}
     * to the memory pointed to by this {@link MachineSizedUIntPtr}. Does 
     * unsigned {@code int} to {@code long} conversion if running on a 64-bit 
     * platform.
     * 
     * @param src the source.
     * @param offset the offset within the source array to start copying from.
     * @param count the number of elements to copy.
     */
public void set(int[] src, int offset, int count) {
    if (_sizeOf() == 4) {
        asIntBuffer(count).put(src, offset, count);
    } else {
        Arrays.checkOffsetAndCount(src.length, offset, count);
        LongBuffer buf = asLongBuffer(count);
        for (int i = 0; i < count; i++) {
            buf.put(((long) src[i + offset]) & 0xffffffffL);
        }
    }
}
Also used : LongBuffer(java.nio.LongBuffer)

Example 8 with LongBuffer

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

the class ByteBufferTest method testAsLongBuffer.

public void testAsLongBuffer() {
    LongBuffer longBuffer;
    byte[] bytes = new byte[8];
    long value;
    // test BIG_ENDIAN long buffer, read
    buf.clear();
    buf.order(ByteOrder.BIG_ENDIAN);
    longBuffer = buf.asLongBuffer();
    assertSame(ByteOrder.BIG_ENDIAN, longBuffer.order());
    while (longBuffer.remaining() > 0) {
        buf.get(bytes);
        value = longBuffer.get();
        assertEquals(bytes2long(bytes, buf.order()), value);
    }
    // test LITTLE_ENDIAN long buffer, read
    buf.clear();
    buf.order(ByteOrder.LITTLE_ENDIAN);
    longBuffer = buf.asLongBuffer();
    assertSame(ByteOrder.LITTLE_ENDIAN, longBuffer.order());
    while (longBuffer.remaining() > 0) {
        buf.get(bytes);
        value = longBuffer.get();
        assertEquals(bytes2long(bytes, buf.order()), value);
    }
    if (!buf.isReadOnly()) {
        // test BIG_ENDIAN long buffer, write
        buf.clear();
        buf.order(ByteOrder.BIG_ENDIAN);
        longBuffer = buf.asLongBuffer();
        assertSame(ByteOrder.BIG_ENDIAN, longBuffer.order());
        while (longBuffer.remaining() > 0) {
            value = (long) longBuffer.remaining();
            longBuffer.put(value);
            buf.get(bytes);
            assertTrue(Arrays.equals(bytes, long2bytes(value, buf.order())));
        }
        // test LITTLE_ENDIAN long buffer, write
        buf.clear();
        buf.order(ByteOrder.LITTLE_ENDIAN);
        longBuffer = buf.asLongBuffer();
        assertSame(ByteOrder.LITTLE_ENDIAN, longBuffer.order());
        while (longBuffer.remaining() > 0) {
            value = (long) longBuffer.remaining();
            longBuffer.put(value);
            buf.get(bytes);
            assertTrue(Arrays.equals(bytes, long2bytes(value, buf.order())));
        }
    }
    buf.clear();
    buf.order(ByteOrder.BIG_ENDIAN);
}
Also used : LongBuffer(java.nio.LongBuffer)

Example 9 with LongBuffer

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

the class LongBufferTest method testCompact.

public void testCompact() {
    // case: buffer is full
    buf.clear();
    buf.mark();
    loadTestData1(buf);
    LongBuffer ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), buf.capacity());
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 0, buf.capacity());
    try {
        buf.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
    // case: buffer is empty
    buf.position(0);
    buf.limit(0);
    buf.mark();
    ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), 0);
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 0, buf.capacity());
    try {
        buf.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
    // case: normal
    assertTrue(buf.capacity() > 5);
    buf.position(1);
    buf.limit(5);
    buf.mark();
    ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), 4);
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 1, 4);
    try {
        buf.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
}
Also used : LongBuffer(java.nio.LongBuffer) InvalidMarkException(java.nio.InvalidMarkException)

Example 10 with LongBuffer

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

the class LongBufferTest method testGetlongArray.

/*
     * Class under test for java.nio.LongBuffer get(long[])
     */
public void testGetlongArray() {
    long[] array = new long[1];
    buf.clear();
    for (int i = 0; i < buf.capacity(); i++) {
        assertEquals(buf.position(), i);
        LongBuffer ret = buf.get(array);
        assertEquals(array[0], buf.get(i));
        assertSame(ret, buf);
    }
    try {
        buf.get(array);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (BufferUnderflowException e) {
    // expected
    }
    try {
        buf.position(buf.limit());
        buf.get((long[]) null);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : LongBuffer(java.nio.LongBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Aggregations

LongBuffer (java.nio.LongBuffer)49 ByteBuffer (java.nio.ByteBuffer)16 DoubleBuffer (java.nio.DoubleBuffer)10 FloatBuffer (java.nio.FloatBuffer)10 IntBuffer (java.nio.IntBuffer)10 ShortBuffer (java.nio.ShortBuffer)10 CharBuffer (java.nio.CharBuffer)9 Test (org.junit.Test)7 BufferOverflowException (java.nio.BufferOverflowException)4 BufferUnderflowException (java.nio.BufferUnderflowException)3 Bitmap (android.graphics.Bitmap)2 InvalidMarkException (java.nio.InvalidMarkException)2 ByteSource (org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource)2 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)2 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)2 UnitTest (org.apache.geode.test.junit.categories.UnitTest)2 Buffer (java.nio.Buffer)1 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)1 BytePointer (org.bytedeco.javacpp.BytePointer)1 DoublePointer (org.bytedeco.javacpp.DoublePointer)1