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

The following examples show how to use java.nio.FloatBuffer#hasArray() . 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: ChannelHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * ByteChannelからfloat配列を読み込む
 * @param channel
 * @return
 * @throws IOException
 */
public static float[] readFloatArray(@NonNull final ByteChannel channel)
	throws IOException {
	
	final int n = readInt(channel);
	final ByteBuffer buf = ByteBuffer.allocate(n * 4).order(ByteOrder.BIG_ENDIAN);
	final int readBytes = channel.read(buf);
	if (readBytes != n * 4) throw new IOException();
	buf.clear();
	final FloatBuffer result = buf.asFloatBuffer();
	if (result.hasArray()) {
		return result.array();
	}  else {
		final float[] b = new float[n];
		result.get(b);
		return b;
	}
}
 
Example 2
Source File: ThriftBmiBridge.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static float[] bufferToFloatArray(ByteBuffer buffer) {
	buffer.order(ByteOrder.nativeOrder());
	FloatBuffer floats = buffer.asFloatBuffer();

	if (floats.hasArray()) {
		return floats.array();
	} else {
		float[] resultArray = new float[floats.capacity()];
		floats.get(resultArray);
		return resultArray;
	}
}
 
Example 3
Source File: FloatPointer.java    From tapir with MIT License 5 votes vote down vote up
/**
 * For direct buffers, calls {@link Pointer#Pointer(Buffer)}, while for buffers
 * backed with an array, allocates enough memory for the array and copies it.
 *
 * @param buffer the Buffer to reference or copy
 * @see #put(float[])
 */
public FloatPointer(FloatBuffer buffer) {
    super(buffer);
    if (buffer != null && buffer.hasArray()) {
        float[] array = buffer.array();
        allocateArray(array.length);
        put(array);
        position(buffer.position());
        limit(buffer.limit());
    }
}
 
Example 4
Source File: TestPointerToBuffer.java    From jcuda with MIT License 5 votes vote down vote up
/**
 * Copy data from the given buffer into an array, using the given
 * pointer, and return whether the array contents matches the
 * expected result
 * 
 * @param buffer The buffer
 * @param pointer The pointer
 * @param elements The number of elements
 * @param expected The expected result
 * @return Whether the contents of the array matched the expected result
 */
private static boolean copy(
    FloatBuffer buffer, Pointer pointer, int elements, float expected[])
{
    log("Buffer     : " + buffer);
    log("position   : " + buffer.position());
    log("limit      : " + buffer.limit());
    if (buffer.hasArray())
    {
        log("arrayOffset: " + buffer.arrayOffset() + " ");
        log("array      : " + Arrays.toString(buffer.array()));
    }

    String contents = "contents   : ";
    for (int i = buffer.position(); i < buffer.limit(); i++)
    {
        contents += buffer.get(i);
        if (i < buffer.limit() - 1)
        {
            contents += ", ";
        }
    }
    log(contents+"\n");

    float result[] = new float[elements];
    cudaMemcpy(Pointer.to(result), pointer, 
        elements * Sizeof.FLOAT, cudaMemcpyHostToHost);

    boolean passed = Arrays.equals(result, expected);
    log("result     : " + Arrays.toString(result));
    log("passed?    : " + passed);
    return passed;
}