I need to create a FloatBuffer from a dynamic set of floats (that is, I don't know the length ahead of time). The only way I found this is rather inelegant (below). I assume that I am missing something and should have a cleaner / simpler method.
My decision:
Vector<Float> temp = new Vector<Float>();
ByteBuffer bb = ByteBuffer.allocateDirect( work.size() * 4 );
bb.order( ByteOrder.nativeOrder() );
FloatBuffer floatBuf = bb.asFloatBuffer();
for( Float f : work )
floatBuf.put( f );
floatBuf.position(0);
I use my buffers for OpenGL commands, so I need to support them (i.e. the resulting FloatBuffer is not just temporary space).
source
share