33import com .scalableminds .zarrjava .ZarrException ;
44import com .scalableminds .zarrjava .store .StoreHandle ;
55import com .scalableminds .zarrjava .v3 .ArrayMetadata ;
6+ import com .scalableminds .zarrjava .v3 .ArrayMetadata .CoreArrayMetadata ;
67import java .nio .ByteBuffer ;
78import java .util .Arrays ;
89import javax .annotation .Nonnull ;
@@ -72,6 +73,28 @@ BytesBytesCodec[] getBytesBytesCodecs() {
7273 .toArray (BytesBytesCodec []::new );
7374 }
7475
76+ public boolean supportsPartialDecode () {
77+ return codecs .length == 1 && codecs [0 ] instanceof ArrayBytesCodec .WithPartialDecode ;
78+ }
79+
80+ @ Nonnull
81+ public Array decodePartial (
82+ @ Nonnull StoreHandle storeHandle ,
83+ long [] offset , int [] shape ,
84+ @ Nonnull ArrayMetadata .CoreArrayMetadata arrayMetadata
85+ ) throws ZarrException {
86+ if (!supportsPartialDecode ()) {
87+ throw new ZarrException (
88+ "Partial decode is not supported for these codecs. " + Arrays .toString (codecs ));
89+ }
90+ Array chunkArray = ((ArrayBytesCodec .WithPartialDecode ) getArrayBytesCodec ()).decodePartial (
91+ storeHandle , offset , shape , arrayMetadata );
92+ if (chunkArray == null ) {
93+ throw new ZarrException ("chunkArray is null. This is likely a bug in one of the codecs." );
94+ }
95+ return chunkArray ;
96+ }
97+
7598 @ Nonnull
7699 public Array decode (
77100 @ Nonnull ByteBuffer chunkBytes ,
@@ -80,9 +103,13 @@ public Array decode(
80103 if (chunkBytes == null ) {
81104 throw new ZarrException ("chunkBytes is null. Ohh nooo." );
82105 }
83- for (BytesBytesCodec codec : getBytesBytesCodecs ()) { // TODO iterate in reverse
106+
107+ BytesBytesCodec [] bytesBytesCodecs = getBytesBytesCodecs ();
108+ for (int i = bytesBytesCodecs .length - 1 ; i >= 0 ; --i ) {
109+ BytesBytesCodec codec = bytesBytesCodecs [i ];
84110 chunkBytes = codec .decode (chunkBytes , arrayMetadata );
85111 }
112+
86113 if (chunkBytes == null ) {
87114 throw new ZarrException (
88115 "chunkBytes is null. This is likely a bug in one of the codecs. " + Arrays .toString (
@@ -92,9 +119,13 @@ public Array decode(
92119 if (chunkArray == null ) {
93120 throw new ZarrException ("chunkArray is null. This is likely a bug in one of the codecs." );
94121 }
95- for (ArrayArrayCodec codec : getArrayArrayCodecs ()) { // TODO iterate in reverse
122+
123+ ArrayArrayCodec [] arrayArrayCodecs = getArrayArrayCodecs ();
124+ for (int i = arrayArrayCodecs .length - 1 ; i >= 0 ; --i ) {
125+ ArrayArrayCodec codec = arrayArrayCodecs [i ];
96126 chunkArray = codec .decode (chunkArray , arrayMetadata );
97127 }
128+
98129 if (chunkArray == null ) {
99130 throw new ZarrException ("chunkArray is null. This is likely a bug in one of the codecs." );
100131 }
@@ -117,6 +148,14 @@ public ByteBuffer encode(
117148 return chunkBytes ;
118149 }
119150
151+ public long computeEncodedSize (long inputByteLength , CoreArrayMetadata arrayMetadata )
152+ throws ZarrException {
153+ for (Codec codec : codecs ) {
154+ inputByteLength = codec .computeEncodedSize (inputByteLength , arrayMetadata );
155+ }
156+ return inputByteLength ;
157+ }
158+
120159 public Array partialDecode (
121160 StoreHandle valueHandle , long [] offset , int [] shape ,
122161 ArrayMetadata .CoreArrayMetadata arrayMetadata
0 commit comments