44import com .amazonaws .auth .AnonymousAWSCredentials ;
55import com .amazonaws .services .s3 .AmazonS3ClientBuilder ;
66import com .fasterxml .jackson .databind .ObjectMapper ;
7+ import com .github .luben .zstd .ZstdCompressCtx ;
8+ import com .github .luben .zstd .ZstdInputStream ;
79import com .github .luben .zstd .ZstdOutputStream ;
810import dev .zarr .zarrjava .store .FilesystemStore ;
911import dev .zarr .zarrjava .store .HttpStore ;
1921import org .junit .jupiter .params .ParameterizedTest ;
2022import org .junit .jupiter .params .provider .CsvSource ;
2123import org .junit .jupiter .params .provider .ValueSource ;
24+ import com .github .luben .zstd .Zstd ;
25+ import ucar .ma2 .MAMath ;
2226
27+ import java .io .FileOutputStream ;
28+ import java .nio .ByteBuffer ;
2329import java .io .*;
2430import java .nio .ByteBuffer ;
2531import java .nio .file .Files ;
@@ -36,9 +42,11 @@ public class ZarrTest {
3642
3743 final static Path TESTDATA = Paths .get ("testdata" );
3844 final static Path TESTOUTPUT = Paths .get ("testoutput" );
39- final static Path ZARRITA_WRITE_PATH = Paths .get ("src/test/java/dev/zarr/zarrjava/zarrita_write.py" );
40- final static Path ZARRITA_READ_PATH = Paths .get ("src/test/java/dev/zarr/zarrjava/zarrita_read.py" );
41- final static Path TEST_ZSTD_LIBRARY_PATH = Paths .get ("src/test/java/dev/zarr/zarrjava/test_zstd_library.py" );
45+ final static Path TEST_PATH = Paths .get ("src/test/java/dev/zarr/zarrjava/" );
46+
47+ final static Path ZARRITA_WRITE_PATH = TEST_PATH .resolve ("zarrita_write.py" );
48+ final static Path ZARRITA_READ_PATH = TEST_PATH .resolve ("zarrita_read.py" );
49+ final static Path TEST_ZSTD_LIBRARY_PATH = TEST_PATH .resolve ("test_zstd_library.py" );
4250
4351 public static String pythonPath () {
4452 if (System .getProperty ("os.name" ).startsWith ("Windows" )) {
@@ -93,6 +101,49 @@ public void testReadFromZarrita(String codec) throws IOException, ZarrException,
93101 Assertions .assertArrayEquals (expectedData , (int []) result .get1DJavaArray (ucar .ma2 .DataType .INT ));
94102 }
95103
104+ private void copy (InputStream inputStream , OutputStream outputStream ) throws IOException {
105+ byte [] buffer = new byte [4096 ];
106+ int len ;
107+ while ((len = inputStream .read (buffer )) > 0 ) {
108+ outputStream .write (buffer , 0 , len );
109+ }
110+ }
111+
112+ @ CsvSource ({"0,true" , "0,false" , "5, true" , "5, false" })
113+ @ ParameterizedTest
114+ public void testZstdLibrary2 (int clevel , boolean checksumFlag ) throws IOException , InterruptedException , ZarrException {
115+ //compress using ZstdCompressCtx
116+ int number = 123456 ;
117+ byte [] src = ByteBuffer .allocate (4 ).putInt (number ).array ();
118+ byte [] compressed ;
119+ try (ZstdCompressCtx ctx = new ZstdCompressCtx ()) {
120+ ctx .setLevel (clevel );
121+ ctx .setChecksum (checksumFlag );
122+ compressed = ctx .compress (src );
123+ }
124+ //decompress with Zstd.decompress
125+ long originalSize = Zstd .decompressedSize (compressed );
126+ byte [] decompressed = Zstd .decompress (compressed , (int ) originalSize );
127+ Assertions .assertEquals (number , ByteBuffer .wrap (decompressed ).getInt ());
128+
129+ //write compressed to file
130+ String compressedDataPath =TESTOUTPUT .resolve ("compressed" + clevel + checksumFlag + ".bin" ).toString ();
131+ try (FileOutputStream fos = new FileOutputStream (compressedDataPath )) {
132+ fos .write (compressed );
133+ }
134+
135+ //decompress in python
136+ Process process = new ProcessBuilder (
137+ pythonPath (),
138+ TEST_PATH .resolve ("decompress_print.py" ).toString (),
139+ compressedDataPath ,
140+ Integer .toString (number )
141+ ).start ();
142+ int exitCode = process .waitFor ();
143+ assert exitCode == 0 ;
144+ }
145+
146+
96147 @ ParameterizedTest
97148 @ CsvSource ({"0,true" , "0,false" , "5, true" , "5, false" })
98149 public void testZstdLibrary (int clevel , boolean checksum ) throws IOException , InterruptedException {
@@ -295,8 +346,8 @@ public void testTransposeCodec() throws ZarrException {
295346 transposeCodecWrongOrder2 .setCoreArrayMetadata (metadata );
296347 transposeCodecWrongOrder3 .setCoreArrayMetadata (metadata );
297348
298- assert ucar . ma2 . MAMath .equals (testDataTransposed120 , transposeCodec .encode (testData ));
299- assert ucar . ma2 . MAMath .equals (testData , transposeCodec .decode (testDataTransposed120 ));
349+ assert MAMath .equals (testDataTransposed120 , transposeCodec .encode (testData ));
350+ assert MAMath .equals (testData , transposeCodec .decode (testDataTransposed120 ));
300351 assertThrows (ZarrException .class , () -> transposeCodecWrongOrder1 .encode (testData ));
301352 assertThrows (ZarrException .class , () -> transposeCodecWrongOrder2 .encode (testData ));
302353 assertThrows (ZarrException .class , () -> transposeCodecWrongOrder3 .encode (testData ));
0 commit comments