Skip to content

Commit 2d8b7b2

Browse files
committed
cleanup code
1 parent 26964dc commit 2d8b7b2

File tree

6 files changed

+8
-91
lines changed

6 files changed

+8
-91
lines changed

src/main/java/dev/zarr/zarrjava/v3/codec/core/ZstdCodec.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,14 @@ private void copy(InputStream inputStream, OutputStream outputStream) throws IOE
4040

4141
@Override
4242
public ByteBuffer decode(ByteBuffer compressedBytes) throws ZarrException {
43-
// Extract the byte array from the ByteBuffer
44-
byte[] compressedArray = new byte[compressedBytes.remaining()];
45-
compressedBytes.get(compressedArray);
43+
byte[] compressedArray = compressedBytes.array();
4644

47-
// Determine the original size (optional: you might need to store the original size separately)
4845
long originalSize = Zstd.decompressedSize(compressedArray);
4946
if (originalSize == 0) {
5047
throw new ZarrException("Failed to get decompressed size");
5148
}
5249

53-
// Create a buffer for the decompressed data
54-
byte[] decompressed = new byte[(int) originalSize];
55-
56-
// Perform decompression
57-
long bytesDecompressed = Zstd.decompress(decompressed, compressedArray);
58-
if (bytesDecompressed != originalSize) {
59-
throw new ZarrException("Decompression failed, incorrect decompressed size");
60-
}
61-
62-
// Wrap the decompressed byte array into a ByteBuffer
50+
byte[] decompressed = Zstd.decompress(compressedArray, (int) originalSize);
6351
return ByteBuffer.wrap(decompressed);
6452
}
6553

src/test/java/dev/zarr/zarrjava/ZarrTest.java

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
import com.github.luben.zstd.ZstdCompressCtx;
8-
import com.github.luben.zstd.ZstdInputStream;
98
import com.github.luben.zstd.ZstdOutputStream;
109
import dev.zarr.zarrjava.store.FilesystemStore;
1110
import dev.zarr.zarrjava.store.HttpStore;
@@ -27,7 +26,6 @@
2726
import java.io.FileOutputStream;
2827
import java.nio.ByteBuffer;
2928
import java.io.*;
30-
import java.nio.ByteBuffer;
3129
import java.nio.file.Files;
3230
import java.nio.file.Path;
3331
import java.nio.file.Paths;
@@ -42,11 +40,7 @@ public class ZarrTest {
4240

4341
final static Path TESTDATA = Paths.get("testdata");
4442
final static Path TESTOUTPUT = Paths.get("testoutput");
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");
43+
final static Path PYTHON_TEST_PATH = Paths.get("src/test/python-scripts/");
5044

5145
public static String pythonPath() {
5246
if (System.getProperty("os.name").startsWith("Windows")) {
@@ -70,7 +64,7 @@ public static void clearTestoutputFolder() throws IOException {
7064
public void testReadFromZarrita(String codec) throws IOException, ZarrException, InterruptedException {
7165

7266
String command = pythonPath();
73-
ProcessBuilder pb = new ProcessBuilder(command, ZARRITA_WRITE_PATH.toString(), codec, TESTOUTPUT.toString());
67+
ProcessBuilder pb = new ProcessBuilder(command, PYTHON_TEST_PATH.resolve("zarrita_write.py").toString(), codec, TESTOUTPUT.toString());
7468
Process process = pb.start();
7569

7670
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
@@ -109,9 +103,9 @@ private void copy(InputStream inputStream, OutputStream outputStream) throws IOE
109103
}
110104
}
111105

112-
@CsvSource({"0,true", "0,false", "5, true", "5, false"})
106+
@CsvSource({"0,true", "0,false", "5, true", "10, false"})
113107
@ParameterizedTest
114-
public void testZstdLibrary2(int clevel, boolean checksumFlag) throws IOException, InterruptedException, ZarrException {
108+
public void testZstdLibrary(int clevel, boolean checksumFlag) throws IOException, InterruptedException, ZarrException {
115109
//compress using ZstdCompressCtx
116110
int number = 123456;
117111
byte[] src = ByteBuffer.allocate(4).putInt(number).array();
@@ -135,49 +129,14 @@ public void testZstdLibrary2(int clevel, boolean checksumFlag) throws IOExceptio
135129
//decompress in python
136130
Process process = new ProcessBuilder(
137131
pythonPath(),
138-
TEST_PATH.resolve("decompress_print.py").toString(),
132+
PYTHON_TEST_PATH.resolve("zstd_decompress.py").toString(),
139133
compressedDataPath,
140134
Integer.toString(number)
141135
).start();
142136
int exitCode = process.waitFor();
143137
assert exitCode == 0;
144138
}
145139

146-
147-
@ParameterizedTest
148-
@CsvSource({"0,true", "0,false", "5, true", "5, false"})
149-
public void testZstdLibrary(int clevel, boolean checksum) throws IOException, InterruptedException {
150-
String zstd_file = TESTOUTPUT + "/testZstdLibrary" + clevel + checksum + ".zstd";
151-
152-
ByteBuffer testBytes = ByteBuffer.allocate(1024);
153-
testBytes.putInt(42);
154-
155-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
156-
ZstdOutputStream zstdStream = new ZstdOutputStream(outputStream, clevel);
157-
zstdStream.setChecksum(checksum);
158-
zstdStream.write(Utils.toArray(testBytes));
159-
zstdStream.close();
160-
ByteBuffer encodedBytes = ByteBuffer.wrap(outputStream.toByteArray());
161-
try (FileOutputStream fileOutputStream = new FileOutputStream(zstd_file)) {
162-
fileOutputStream.write(encodedBytes.array());
163-
}
164-
String command = pythonPath();
165-
ProcessBuilder pb = new ProcessBuilder(command, TEST_ZSTD_LIBRARY_PATH.toString(), zstd_file);
166-
Process process = pb.start();
167-
168-
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
169-
String line;
170-
while ((line = reader.readLine()) != null) {
171-
System.out.println(line);
172-
}
173-
BufferedReader readerErr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
174-
while ((line = readerErr.readLine()) != null) {
175-
System.err.println(line);
176-
}
177-
int exitCode = process.waitFor();
178-
assert exitCode == 0;
179-
}
180-
181140
//TODO: add crc32c
182141
@ParameterizedTest
183142
@ValueSource(strings = {"blosc", "gzip", "zstd", "bytes", "transpose", "sharding_start", "sharding_end"})
@@ -226,7 +185,7 @@ public void testWriteToZarrita(String codec) throws IOException, ZarrException,
226185

227186
String command = pythonPath();
228187

229-
ProcessBuilder pb = new ProcessBuilder(command, ZARRITA_READ_PATH.toString(), codec, TESTOUTPUT.toString());
188+
ProcessBuilder pb = new ProcessBuilder(command, PYTHON_TEST_PATH.resolve("zarrita_read.py").toString(), codec, TESTOUTPUT.toString());
230189
Process process = pb.start();
231190

232191
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

src/test/java/dev/zarr/zarrjava/test_zstd_library.py

Lines changed: 0 additions & 30 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)