Java Code Examples for java.nio.FloatBuffer#slice()

The following examples show how to use java.nio.FloatBuffer#slice() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestPointerToBuffer.java    From jcuda with MIT License 6 votes vote down vote up
@Test
public void testWithSliceAtOffset2()
{
    float array[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
    FloatBuffer arrayBuffer = FloatBuffer.wrap(array);
    FloatBuffer directBuffer = 
        ByteBuffer.allocateDirect(array.length * Sizeof.FLOAT).
            order(ByteOrder.nativeOrder()).asFloatBuffer();
    directBuffer.put(array);
    directBuffer.rewind();
    
    arrayBuffer.position(2);
    directBuffer.position(2);

    FloatBuffer arraySlice = arrayBuffer.slice();
    FloatBuffer directSlice = directBuffer.slice();
    
    assertTrue(copyWithTo      (arraySlice,  4, new float[] {0, 1, 2, 3}));
    assertTrue(copyWithToBuffer(arraySlice,  4, new float[] {2, 3, 4, 5}));
    assertTrue(copyWithTo      (directSlice, 4, new float[] {2, 3, 4, 5}));
    assertTrue(copyWithToBuffer(directSlice, 4, new float[] {2, 3, 4, 5}));
}
 
Example 2
Source File: TestPointerToBuffer.java    From jcuda with MIT License 6 votes vote down vote up
@Test
public void testWithSliceAtOffset2WithPosition2()
{
    float array[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
    FloatBuffer arrayBuffer = FloatBuffer.wrap(array);
    FloatBuffer directBuffer = 
        ByteBuffer.allocateDirect(array.length * Sizeof.FLOAT).
            order(ByteOrder.nativeOrder()).asFloatBuffer();
    directBuffer.put(array);
    directBuffer.rewind();
    
    arrayBuffer.position(2);
    directBuffer.position(2);

    FloatBuffer arraySlice = arrayBuffer.slice();
    FloatBuffer directSlice = directBuffer.slice();
    
    arraySlice.position(2);
    directSlice.position(2);
    
    assertTrue(copyWithTo      (arraySlice,  4, new float[] {0, 1, 2, 3}));
    assertTrue(copyWithToBuffer(arraySlice,  4, new float[] {4, 5, 6, 7}));
    assertTrue(copyWithTo      (directSlice, 4, new float[] {2, 3, 4, 5}));
    assertTrue(copyWithToBuffer(directSlice, 4, new float[] {4, 5, 6, 7}));
}
 
Example 3
Source File: FloatNioDataBuffer.java    From java with Apache License 2.0 5 votes vote down vote up
@Override
public FloatDataBuffer slice(long index, long size) {
  Validator.sliceArgs(this, index, size);
  FloatBuffer sliceBuf = buf.duplicate();
  sliceBuf.position((int)index);
  sliceBuf.limit((int)index + (int)size);
  return new FloatNioDataBuffer(sliceBuf.slice());
}
 
Example 4
Source File: TestPointerToBuffer.java    From jcuda with MIT License 4 votes vote down vote up
/**
 * Entry point of this test
 * 
 * @param args Not used
 */
public static void main(String[] args)
{
    PRINT_LOG_MESSAGES = true;
    
    // Create an array-backed float buffer containing values 0 to 7
    float array[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
    FloatBuffer arrayBuffer = FloatBuffer.wrap(array);
    
    // Create a direct float buffer containing the same values
    FloatBuffer directBuffer = 
        ByteBuffer.allocateDirect(array.length * Sizeof.FLOAT).
            order(ByteOrder.nativeOrder()).asFloatBuffer();
    directBuffer.put(array);
    directBuffer.rewind();
    
    // We're optimistic.
    boolean passed = true;
    
    // Copy 4 elements of the buffer into an array.
    // The array will contain the first 4 elements.
    log("\nCopy original buffer");
    passed &= copyWithTo      (arrayBuffer,  4, new float[] {0, 1, 2, 3});
    passed &= copyWithToBuffer(arrayBuffer,  4, new float[] {0, 1, 2, 3});
    passed &= copyWithTo      (directBuffer, 4, new float[] {0, 1, 2, 3});
    passed &= copyWithToBuffer(directBuffer, 4, new float[] {0, 1, 2, 3});
    
    // Advance the buffer position, and copy 4 elements
    // into an array. The Pointer#to(Buffer) method will
    // ignore the position, and thus again copy the first
    // 4 elements. The Pointer#toBuffer(Buffer) method
    // will take the position into account, and thus copy
    // the elements 2,3,4,5
    log("\nCopy buffer with position 2");
    arrayBuffer.position(2);
    directBuffer.position(2);
    passed &= copyWithTo      (arrayBuffer,  4, new float[] {0, 1, 2, 3});
    passed &= copyWithToBuffer(arrayBuffer,  4, new float[] {2, 3, 4, 5});
    passed &= copyWithTo      (directBuffer, 4, new float[] {0, 1, 2, 3});
    passed &= copyWithToBuffer(directBuffer, 4, new float[] {2, 3, 4, 5});
    
    // Create a slice of the buffer, and copy 4 elements 
    // of the slice into an array. The slice will contain
    // the 6 remaining elements of the buffer: 2,3,4,5,6,7.
    // The Pointer#to method will 
    // - ignore slice offset for buffers with backing arrays
    // - consider the slice offset for direct buffers
    // The Pointer#toBuffer method will take the slice offset into
    // account in any case
    log("\nCopy slice with offset 2");
    FloatBuffer arraySlice = arrayBuffer.slice();
    FloatBuffer directSlice = directBuffer.slice();
    passed &= copyWithTo      (arraySlice,  4, new float[] {0, 1, 2, 3});
    passed &= copyWithToBuffer(arraySlice,  4, new float[] {2, 3, 4, 5});
    passed &= copyWithTo      (directSlice, 4, new float[] {2, 3, 4, 5});
    passed &= copyWithToBuffer(directSlice, 4, new float[] {2, 3, 4, 5});

    // Set the position of the slice to 2. 
    // The Pointer#to method will 
    // - ignore slice offset and position for buffers with backing arrays
    // - consider the slice offset, but not the position for direct buffers
    // The Pointer#toBuffer method will take the slice offset 
    // and positions into account in any case
    log("\nCopy slice with offset 2 and position 2");
    arraySlice.position(2);
    directSlice.position(2);
    passed &= copyWithTo      (arraySlice,  4, new float[] {0, 1, 2, 3});
    passed &= copyWithToBuffer(arraySlice,  4, new float[] {4, 5, 6, 7});
    passed &= copyWithTo      (directSlice, 4, new float[] {2, 3, 4, 5});
    passed &= copyWithToBuffer(directSlice, 4, new float[] {4, 5, 6, 7});
    
    if (passed)
    {
        log("\nPASSED");
    }
    else
    {
        log("\nFAILED");
    }
}