Skip to content

Commit 94e9e37

Browse files
committed
fix BytesCodec encode for bool, float, double
1 parent 88543e6 commit 94e9e37

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

src/main/java/dev/zarr/zarrjava/core/codec/core/BytesCodec.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonValue;
44
import dev.zarr.zarrjava.core.codec.ArrayBytesCodec;
5-
import ucar.ma2.Array;
6-
import ucar.ma2.DataType;
7-
import ucar.ma2.Index;
5+
import ucar.ma2.*;
86

97
import java.nio.ByteBuffer;
108
import java.nio.ByteOrder;
@@ -34,9 +32,44 @@ public Array decode(ByteBuffer chunkBytes) {
3432
}
3533
@Override
3634
public ByteBuffer encode(Array chunkArray) {
37-
return chunkArray.getDataAsByteBuffer(getByteOrder());
38-
}
35+
ByteOrder order = getByteOrder();
36+
37+
// Boolean
38+
if (chunkArray instanceof ArrayBoolean) {
39+
boolean[] data = (boolean[]) chunkArray.copyTo1DJavaArray();
40+
ByteBuffer bb = ByteBuffer.allocate(data.length);
41+
for (boolean b : data) {
42+
bb.put((byte) (b ? 1 : 0));
43+
}
44+
bb.flip();
45+
return bb;
46+
}
3947

48+
// Float32
49+
if (chunkArray instanceof ArrayFloat) {
50+
float[] data = (float[]) chunkArray.copyTo1DJavaArray();
51+
ByteBuffer bb = ByteBuffer.allocate(data.length * Float.BYTES).order(order);
52+
for (float f : data) {
53+
bb.putFloat(f);
54+
}
55+
bb.flip();
56+
return bb;
57+
}
58+
59+
// Float64
60+
if (chunkArray instanceof ArrayDouble) {
61+
double[] data = (double[]) chunkArray.copyTo1DJavaArray();
62+
ByteBuffer bb = ByteBuffer.allocate(data.length * Double.BYTES).order(order);
63+
for (double d : data) {
64+
bb.putDouble(d);
65+
}
66+
bb.flip();
67+
return bb;
68+
}
69+
70+
// All other primitive types (byte, short, int, long, char)
71+
return chunkArray.getDataAsByteBuffer(order);
72+
}
4073
public enum Endian {
4174
LITTLE("little"),
4275
BIG("big");

0 commit comments

Comments
 (0)