1

I've been searching for a way to convert a FloatBuffer array to a byte array. I have found a way to convert a FloatBuffer object to byte[]: convert from floatbuffer to byte[]

But after searching the Internet for several hours I haven't been able to find something equivalent to convert from FloatBuffer[].

And to do the inverse, to convert from byte[] to FloatBuffer[], I've only found this:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(floatBufferObject);
byte [] descriptorsBytes = bos.toByteArray();

But it seems a little strange there is not a simpler way to do this. Maybe I'm missing something very obvious, maybe I should convert the FloatBuffer array to other type that is simpler to convert to a byte array?

Community
  • 1
  • 1

3 Answers3

3

Do you want one FloatBuffer, or multiple?

To convert from a FloatBuffer to a byte[], you could do something like

 FloatBuffer input;
 byte[] output = new byte[input.capacity() * 4];
 ByteBuffer.wrap(output).asFloatBuffer().put(input);

The other direction would just be

 ByteBuffer.wrap(byteArray).asFloatBuffer()
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • The problem is I don't have a simple FloatBuffer, I have an array of FloatBuffer objects. This "FloatBuffer[]" object doesn't have methods like "get", "put", "wrap". Only each row of the FloatBuffer Array, that can be accessed as floatBufferArray[i] has those methods avaible. – Maria Garcia Feb 28 '13 at 04:09
  • Then you'll have to do it one at a time, but these implementations should give you an idea as to how to convert each row between `FloatBuffer` and bytes. – Louis Wasserman Feb 28 '13 at 04:12
  • But, because I need the byte array to store this info in a database, I have to store this as just one byte array, not one for each FloatBuffer row of the FloatBuffer array. So if I do it one FloatBuffer at a time, I'll have multiple byte arrays... – Maria Garcia Feb 28 '13 at 04:15
  • Create one big `ByteBuffer`, call its `asFloatBuffer()` method, and then put each of the small `FloatBuffer`s into that big `FloatBuffer`. – Louis Wasserman Feb 28 '13 at 04:38
  • Thanks, it works. The conversion in the other direction works too, with ByteBuffer.wrap(byteArray).asFloatBuffer(); but I obtain the result as just a FloatBuffer, not a FloatBuffer array. Is there a way to obtain each of the original FloatBuffer rows separated? – Maria Garcia Feb 28 '13 at 05:56
1

You already had the answer on how to convert one FloatBuffer into a byte array, so simply extend that to convert an array of them:

final FloatBuffer[] floatBuffers = new FloatBuffer[] {...};
final ByteBuffer byteBuffer = ByteBuffer.allocate(sumOfFloatBufferCapacities) * 4);
final FloatBuffer floatBufView = byteBuffer.asFloatBuffer();
for (final FloatBuffer fBuf : floatBuffers) {
    floatBufView.put(fBuf);
}
byte[] data = byteBuffer.array();

The above is pseudocode, you can adapt it to your needs.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • Thanks, it works. Now I'm trying to convert the byte array back to FloatBuffer[] doing something like this: FloatBuffer fb = ByteBuffer.wrap(byteArray).asFloatBuffer(); But this retrieves the array as just one row. Is there a way to get it as FloatBuffer[] or I have to do another for to divide it in diferent rows...? – Maria Garcia Feb 28 '13 at 05:34
  • @MarciaGarcia - Once you merge the arrays there is no way of determining which floats should go where, unless you keep track externally (say in a list of integers), of the number of floats that should go into each FloatBuffer. – Perception Feb 28 '13 at 06:32
0

Convert FloatBuffer[] to byte[]:

    FloatBuffer[] buffers = ...
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    for (FloatBuffer fb : buffers) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(fb.capacity() * 4);
        byteBuffer.asFloatBuffer().put(fb);
        bos.write(byteBuffer.array());
    }
    byte[] ba = bos.toByteArray();
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275