Skip to content

Commit 9dc2b40

Browse files
committed
fix BytesCodec for boolean
1 parent 54175d2 commit 9dc2b40

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.fasterxml.jackson.annotation.JsonValue;
44
import dev.zarr.zarrjava.core.codec.ArrayBytesCodec;
55
import ucar.ma2.Array;
6+
import ucar.ma2.DataType;
7+
import ucar.ma2.Index;
68

79
import java.nio.ByteBuffer;
810
import java.nio.ByteOrder;
@@ -11,12 +13,25 @@ public abstract class BytesCodec extends ArrayBytesCodec {
1113
protected abstract ByteOrder getByteOrder();
1214

1315
@Override
14-
public Array decode(ByteBuffer chunkBytes) {
15-
chunkBytes.order(getByteOrder());
16-
return Array.factory(arrayMetadata.dataType.getMA2DataType(), arrayMetadata.chunkShape,
17-
chunkBytes);
16+
public Array decode(ByteBuffer chunkBytes) {
17+
chunkBytes.order(getByteOrder());
18+
DataType dtype = arrayMetadata.dataType.getMA2DataType();
19+
int[] shape = arrayMetadata.chunkShape;
20+
21+
// Array.factory does not support boolean arrays directly from ByteBuffer
22+
if (dtype == DataType.BOOLEAN) {
23+
int size = chunkBytes.remaining();
24+
boolean[] bools = new boolean[size];
25+
for (int i = 0; i < size; i++) {
26+
bools[i] = chunkBytes.get(i) != 0;
27+
}
28+
29+
Index index = Index.factory(shape);
30+
return Array.factory(DataType.BOOLEAN, index, bools);
1831
}
1932

33+
return Array.factory(dtype, shape, chunkBytes);
34+
}
2035
@Override
2136
public ByteBuffer encode(Array chunkArray) {
2237
return chunkArray.getDataAsByteBuffer(getByteOrder());

0 commit comments

Comments
 (0)