|
4 | 4 | import com.fasterxml.jackson.annotation.JsonProperty; |
5 | 5 | import com.github.luben.zstd.Zstd; |
6 | 6 | import com.github.luben.zstd.ZstdCompressCtx; |
7 | | -import com.github.luben.zstd.ZstdInputStream; |
8 | | -import com.github.luben.zstd.ZstdOutputStream; |
9 | 7 | import dev.zarr.zarrjava.ZarrException; |
10 | | -import dev.zarr.zarrjava.utils.Utils; |
11 | 8 | import dev.zarr.zarrjava.v3.ArrayMetadata; |
12 | 9 | import dev.zarr.zarrjava.v3.codec.BytesBytesCodec; |
13 | | -import java.io.ByteArrayInputStream; |
14 | | -import java.io.ByteArrayOutputStream; |
| 10 | + |
| 11 | +import javax.annotation.Nonnull; |
15 | 12 | import java.io.IOException; |
16 | 13 | import java.io.InputStream; |
17 | 14 | import java.io.OutputStream; |
18 | 15 | import java.nio.ByteBuffer; |
19 | | -import javax.annotation.Nonnull; |
20 | 16 |
|
21 | 17 | public class ZstdCodec extends BytesBytesCodec { |
22 | 18 |
|
23 | | - public final String name = "zstd"; |
24 | | - @Nonnull |
25 | | - public final Configuration configuration; |
26 | | - |
27 | | - @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) |
28 | | - public ZstdCodec( |
29 | | - @Nonnull @JsonProperty(value = "configuration", required = true) Configuration configuration) { |
30 | | - this.configuration = configuration; |
31 | | - } |
32 | | - |
33 | | - private void copy(InputStream inputStream, OutputStream outputStream) throws IOException { |
34 | | - byte[] buffer = new byte[4096]; |
35 | | - int len; |
36 | | - while ((len = inputStream.read(buffer)) > 0) { |
37 | | - outputStream.write(buffer, 0, len); |
| 19 | + public final String name = "zstd"; |
| 20 | + @Nonnull |
| 21 | + public final Configuration configuration; |
| 22 | + |
| 23 | + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) |
| 24 | + public ZstdCodec( |
| 25 | + @Nonnull @JsonProperty(value = "configuration", required = true) Configuration configuration) { |
| 26 | + this.configuration = configuration; |
| 27 | + } |
| 28 | + |
| 29 | + private void copy(InputStream inputStream, OutputStream outputStream) throws IOException { |
| 30 | + byte[] buffer = new byte[4096]; |
| 31 | + int len; |
| 32 | + while ((len = inputStream.read(buffer)) > 0) { |
| 33 | + outputStream.write(buffer, 0, len); |
| 34 | + } |
38 | 35 | } |
39 | | - } |
40 | 36 |
|
41 | | - @Override |
42 | | - public ByteBuffer decode(ByteBuffer compressedBytes) throws ZarrException { |
43 | | - byte[] compressedArray = compressedBytes.array(); |
| 37 | + @Override |
| 38 | + public ByteBuffer decode(ByteBuffer compressedBytes) throws ZarrException { |
| 39 | + byte[] compressedArray = compressedBytes.array(); |
| 40 | + |
| 41 | + long originalSize = Zstd.decompressedSize(compressedArray); |
| 42 | + if (originalSize == 0) { |
| 43 | + throw new ZarrException("Failed to get decompressed size"); |
| 44 | + } |
44 | 45 |
|
45 | | - long originalSize = Zstd.decompressedSize(compressedArray); |
46 | | - if (originalSize == 0) { |
47 | | - throw new ZarrException("Failed to get decompressed size"); |
| 46 | + byte[] decompressed = Zstd.decompress(compressedArray, (int) originalSize); |
| 47 | + return ByteBuffer.wrap(decompressed); |
48 | 48 | } |
49 | 49 |
|
50 | | - byte[] decompressed = Zstd.decompress(compressedArray, (int) originalSize); |
51 | | - return ByteBuffer.wrap(decompressed); |
52 | | - } |
53 | | - |
54 | | - @Override |
55 | | - public ByteBuffer encode(ByteBuffer chunkBytes) throws ZarrException { |
56 | | - byte[] arr = chunkBytes.array(); |
57 | | - byte[] compressed; |
58 | | - try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { |
59 | | - ctx.setLevel(configuration.level); |
60 | | - ctx.setChecksum(configuration.checksum); |
61 | | - compressed = ctx.compress(arr); |
| 50 | + @Override |
| 51 | + public ByteBuffer encode(ByteBuffer chunkBytes) throws ZarrException { |
| 52 | + byte[] arr = chunkBytes.array(); |
| 53 | + byte[] compressed; |
| 54 | + try (ZstdCompressCtx ctx = new ZstdCompressCtx()) { |
| 55 | + ctx.setLevel(configuration.level); |
| 56 | + ctx.setChecksum(configuration.checksum); |
| 57 | + compressed = ctx.compress(arr); |
| 58 | + } |
| 59 | + return ByteBuffer.wrap(compressed); |
62 | 60 | } |
63 | | - return ByteBuffer.wrap(compressed); |
64 | | - } |
65 | 61 |
|
66 | | - @Override |
67 | | - public long computeEncodedSize(long inputByteLength, |
68 | | - ArrayMetadata.CoreArrayMetadata arrayMetadata) throws ZarrException { |
69 | | - throw new ZarrException("Not implemented for Zstd codec."); |
70 | | - } |
| 62 | + @Override |
| 63 | + public long computeEncodedSize(long inputByteLength, |
| 64 | + ArrayMetadata.CoreArrayMetadata arrayMetadata) throws ZarrException { |
| 65 | + throw new ZarrException("Not implemented for Zstd codec."); |
| 66 | + } |
71 | 67 |
|
72 | | - public static final class Configuration { |
| 68 | + public static final class Configuration { |
73 | 69 |
|
74 | | - public final int level; |
75 | | - public final boolean checksum; |
| 70 | + public final int level; |
| 71 | + public final boolean checksum; |
76 | 72 |
|
77 | | - @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) |
78 | | - public Configuration(@JsonProperty(value = "level", defaultValue = "5") int level, |
79 | | - @JsonProperty(value = "checksum", defaultValue = "true") boolean checksum) |
80 | | - throws ZarrException { |
81 | | - if (level < -131072 || level > 22) { |
82 | | - throw new ZarrException("'level' needs to be between -131072 and 22."); |
83 | | - } |
84 | | - this.level = level; |
85 | | - this.checksum = checksum; |
| 73 | + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) |
| 74 | + public Configuration(@JsonProperty(value = "level", defaultValue = "5") int level, |
| 75 | + @JsonProperty(value = "checksum", defaultValue = "true") boolean checksum) |
| 76 | + throws ZarrException { |
| 77 | + if (level < -131072 || level > 22) { |
| 78 | + throw new ZarrException("'level' needs to be between -131072 and 22."); |
| 79 | + } |
| 80 | + this.level = level; |
| 81 | + this.checksum = checksum; |
| 82 | + } |
86 | 83 | } |
87 | | - } |
88 | 84 | } |
89 | 85 |
|
90 | 86 |
|
0 commit comments