开发者

Fastest way to write a FloatBuffer or Float(float) array to a file in Java

开发者 https://www.devze.com 2023-03-17 06:08 出处:网络
I\'ve got a bunch of float data in a FloatBuffer which needs to be written to a file (without it taking three minutes). Currently a DataOutput开发者_开发技巧Stream is used to write the FloatBuffer ele

I've got a bunch of float data in a FloatBuffer which needs to be written to a file (without it taking three minutes). Currently a DataOutput开发者_开发技巧Stream is used to write the FloatBuffer element by element to a file. This is slow. Preferably, I'd like to be using a FileChannel, but I hit a snag since it seems a FloatBuffer can't be converted to a ByteBuffer and bytes are what the FileChannel needs in order to write data.

Instead of using FoatBuffers as my data source, I could easily be using an array. But I can't easily use a ByteBuffer/array instead.

Any insight into this problem would be much appreciated. Thanks.


Instead of starting off with a FloatBuffer, could you create a ByteBuffer to use for writing to the FileChannel, then use ByteBuffer.asFloatBuffer, write into that FloatBuffer however you're currently doing it, and then write out the ByteBuffer which will then contain the relevant information?

Personally I've always found java.nio rather confusing, but this feels like it probably should work...

EDIT: Another user tried this, and found it not to work:

However, asFloatBuffer() will not work because hasArray() will be false. In short, the following does not work (neither allocate, nor allocateDirect will work):

ByteBuffer.allocate(amount * 4).asFloatBuffer().asArray()

I don't have the time to investigate alternatives right now, unfortunately.

0

精彩评论

暂无评论...
验证码 换一张
取 消