|
1 | 1 | package dev.zarr.zarrjava.core.codec.core; |
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.annotation.JsonValue; |
| 4 | +import dev.zarr.zarrjava.ZarrException; |
4 | 5 | import dev.zarr.zarrjava.core.codec.ArrayBytesCodec; |
5 | 6 | import ucar.ma2.*; |
6 | 7 |
|
7 | 8 | import java.nio.ByteBuffer; |
8 | 9 | import java.nio.ByteOrder; |
9 | 10 |
|
10 | 11 | public abstract class BytesCodec extends ArrayBytesCodec { |
11 | | - protected abstract ByteOrder getByteOrder(); |
| 12 | + protected abstract ByteOrder getByteOrder() throws ZarrException; |
12 | 13 |
|
13 | 14 | @Override |
14 | | -public Array decode(ByteBuffer chunkBytes) { |
15 | | - chunkBytes.order(getByteOrder()); |
16 | | - DataType dtype = arrayMetadata.dataType.getMA2DataType(); |
17 | | - int[] shape = arrayMetadata.chunkShape; |
18 | | - |
19 | | - // Array.factory does not support boolean arrays directly from ByteBuffer |
20 | | - if (dtype == DataType.BOOLEAN) { |
21 | | - int size = chunkBytes.remaining(); |
22 | | - boolean[] bools = new boolean[size]; |
23 | | - for (int i = 0; i < size; i++) { |
24 | | - bools[i] = chunkBytes.get(i) != 0; |
25 | | - } |
| 15 | + public Array decode(ByteBuffer chunkBytes) throws ZarrException { |
| 16 | + ByteOrder order = ByteOrder.BIG_ENDIAN; // Default for 1-byte types |
| 17 | + if (arrayMetadata.dataType.getByteCount() > 1) |
| 18 | + order = getByteOrder(); |
| 19 | + chunkBytes.order(order); |
| 20 | + DataType dtype = arrayMetadata.dataType.getMA2DataType(); |
| 21 | + int[] shape = arrayMetadata.chunkShape; |
| 22 | + |
| 23 | + // Array.factory does not support boolean arrays directly from ByteBuffer |
| 24 | + if (dtype == DataType.BOOLEAN) { |
| 25 | + int size = chunkBytes.remaining(); |
| 26 | + boolean[] bools = new boolean[size]; |
| 27 | + for (int i = 0; i < size; i++) { |
| 28 | + bools[i] = chunkBytes.get(i) != 0; |
| 29 | + } |
26 | 30 |
|
27 | | - Index index = Index.factory(shape); |
28 | | - return Array.factory(DataType.BOOLEAN, index, bools); |
| 31 | + Index index = Index.factory(shape); |
| 32 | + return Array.factory(DataType.BOOLEAN, index, bools); |
| 33 | + } |
| 34 | + return Array.factory(dtype, shape, chunkBytes); |
29 | 35 | } |
30 | 36 |
|
31 | | - return Array.factory(dtype, shape, chunkBytes); |
32 | | -} |
33 | 37 | @Override |
34 | | - public ByteBuffer encode(Array chunkArray) { |
35 | | - ByteOrder order = getByteOrder(); |
| 38 | + public ByteBuffer encode(Array chunkArray) throws ZarrException { |
| 39 | + ByteOrder order = ByteOrder.BIG_ENDIAN; // Default for 1-byte types |
| 40 | + if (arrayMetadata.dataType.getByteCount() > 1) |
| 41 | + order = getByteOrder(); |
36 | 42 |
|
37 | 43 | // Boolean |
38 | 44 | if (chunkArray instanceof ArrayBoolean) { |
@@ -94,6 +100,10 @@ public ByteOrder getByteOrder() { |
94 | 100 | throw new RuntimeException("Unreachable"); |
95 | 101 | } |
96 | 102 | } |
| 103 | + |
| 104 | + public static Endian nativeOrder() { |
| 105 | + return ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN ? LITTLE : BIG; |
| 106 | + } |
97 | 107 | } |
98 | 108 |
|
99 | 109 | } |
0 commit comments