Thanks, but I am still confused. How can a buffer not be backed by an
accessible array?
I would have guessed due to this method here:
<
http://download.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html#allocateDirect(int)>
You'll need to read the comments at the begging of that page to
understand what it is doing.
I tried this code, and hasArray returns true. However, hasArray2
returns false.
byte[] b = new byte[numfloats*4];
dis.read(b, 0, numfloats*4);
ByteBuffer bb = ByteBuffer.wrap(b);
It's been a while since I've worked with ByteBuffers, but I think the
wrap() method is mostly for writing, not reading.
boolean hasArray = bb.hasArray();
FloatBuffer fb2 = bb.asFloatBuffer();
boolean hasArray2 = fb2.hasArray();
I'm trying to get a float[] out of this, but I can't if hasArray2
returns false.
The trick to ByteBuffers is they work with Channels, not streams.
<
http://download.oracle.com/javase/6/docs/api/java/nio/channels/Channel.html>
<
http://download.oracle.com/javase/6/docs/api/java/nio/channels/FileChannel.html>
Not tested, but I think this is the idea:
FileInputStream fis = new FileInputStream( "your/filename/here" );
FileChannel fchan = fis.getChannel();
ByteBuffer inBuff = ByteBuffer.allocateDirect( 64 * 1024 );
fchan.read( inBuff );
FloatBuffer floatBuffer = inBuff.asFloatBuffer()
float[] floats = new float[ numfloats ];
floatBuffer.read( floats );
To reuse the underlying ByteBuffer, I think you have to reset() it. The
FloatBuffer does not have to be reset, it'll reset when you reset the
ByteBuffer backing it, and you shouldn't have to call asFloatBuffer()
again, you can just re-use the existing FloatBuffer. This'll help you
with speed.
You're doing a good job reading the documentation so far, you should
verify what I've done above because I haven't tested it. However I
think it's at least close and gives you the basic pattern for reading
with a ByteBuffer.
If you have more than 16 * 1024 floats, you'll need to use a loop to
read them, or increase the side of the buffer in allocateDirect.