Skip to content

Commit fe2c955

Browse files
dfa1claude
andcommitted
chore: clear IntelliJ inspection warnings in the zstd module
- ZstdException: annotate serialVersionUID with @serial - ZstdInputStream: inCap is constructor-only now, make it a local - ZstdStreamBuffer: drop the set() pos parameter (always 0); set pos internally and update the stream call sites - ZstdSkippableContent: use a record pattern in equals; suppress the IntelliJ @NotNull-override advisory on toString (never returns null, and we don't pull in the JetBrains annotations) - ZstdFrame: the five zero-copy MemorySegment overloads had no caller — cover them with a SegmentOverloads test that checks each mirrors its byte[] counterpart Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent e6a3537 commit fe2c955

8 files changed

Lines changed: 55 additions & 17 deletions

File tree

zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressStream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ private void loadDictionary(ZstdDictionary dictionary) {
9999
public ZstdStreamResult compress(MemorySegment dst, MemorySegment src, ZstdEndDirective directive) {
100100
NativeCall.requireNative(dst, "dst");
101101
NativeCall.requireNative(src, "src");
102-
in.set(src, src.byteSize(), 0);
103-
out.set(dst, dst.byteSize(), 0);
102+
in.set(src, src.byteSize());
103+
out.set(dst, dst.byteSize());
104104
long remaining = NativeCall.checkReturnValue(() -> (long) Bindings.COMPRESS_STREAM2.invokeExact(
105105
ptr(), out.segment(), in.segment(), directive.value()));
106106
return new ZstdStreamResult(in.pos(), out.pos(), remaining);

zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressStream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ private void loadDictionary(ZstdDictionary dictionary) {
7070
/// @return how much was consumed and produced, and the remaining hint
7171
/// @throws ZstdException if the frame is invalid
7272
public ZstdStreamResult decompress(MemorySegment dst, MemorySegment src) {
73-
in.set(src, src.byteSize(), 0);
74-
out.set(dst, dst.byteSize(), 0);
73+
in.set(src, src.byteSize());
74+
out.set(dst, dst.byteSize());
7575
long remaining = NativeCall.checkReturnValue(() -> (long) Bindings.DECOMPRESS_STREAM.invokeExact(
7676
ptr(), out.segment(), in.segment()));
7777
return new ZstdStreamResult(in.pos(), out.pos(), remaining);

zstd/src/main/java/io/github/dfa1/zstd/ZstdException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package io.github.dfa1.zstd;
22

3+
import java.io.Serial;
4+
35
/// Thrown when a zstd native call reports an error.
46
///
57
/// Unchecked: zstd errors on valid use of this API indicate either corrupt
68
/// input or a programming error (e.g. an undersized destination buffer), not a
79
/// recoverable I/O condition.
810
public final class ZstdException extends RuntimeException {
911

12+
@Serial
1013
private static final long serialVersionUID = 1L;
1114

1215
/// The zstd error category for this failure.

zstd/src/main/java/io/github/dfa1/zstd/ZstdInputStream.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public final class ZstdInputStream extends InputStream {
2929
private final MemorySegment dctx;
3030
private final MemorySegment inSeg;
3131
private final MemorySegment outSeg;
32-
private final long inCap;
3332
private final long outCap;
3433
private final ZstdStreamBuffer inBuf = new ZstdStreamBuffer(arena);
3534
private final ZstdStreamBuffer outBufView = new ZstdStreamBuffer(arena);
@@ -67,7 +66,7 @@ public ZstdInputStream(InputStream in, ZstdDictionary dictionary) {
6766
if (dictionary != null) {
6867
loadDictionary(dictionary);
6968
}
70-
this.inCap = (long) Bindings.DSTREAM_IN_SIZE.invokeExact();
69+
long inCap = (long) Bindings.DSTREAM_IN_SIZE.invokeExact();
7170
this.outCap = (long) Bindings.DSTREAM_OUT_SIZE.invokeExact();
7271
this.inSeg = arena.allocate(inCap);
7372
this.outSeg = arena.allocate(outCap);
@@ -140,9 +139,9 @@ private boolean produce() throws IOException {
140139
return false;
141140
}
142141
MemorySegment.copy(feed, 0, inSeg, JAVA_BYTE, 0, r);
143-
inBuf.set(inSeg, r, 0);
142+
inBuf.set(inSeg, r);
144143
}
145-
outBufView.set(outSeg, outCap, 0);
144+
outBufView.set(outSeg, outCap);
146145
lastHint = NativeCall.checkReturnValue(() -> (long) Bindings.DECOMPRESS_STREAM.invokeExact(
147146
dctx, outBufView.segment(), inBuf.segment()));
148147
int produced = Math.toIntExact(outBufView.pos());

zstd/src/main/java/io/github/dfa1/zstd/ZstdOutputStream.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void write(byte[] b, int off, int len) throws IOException {
153153
while (remaining > 0) {
154154
int chunk = (int) Math.min(remaining, inCap);
155155
MemorySegment.copy(b, pos, inSeg, JAVA_BYTE, 0, chunk);
156-
in.set(inSeg, chunk, 0);
156+
in.set(inSeg, chunk);
157157
do {
158158
drainOutput(ZSTD_E_CONTINUE);
159159
} while (in.pos() < chunk);
@@ -165,7 +165,7 @@ public void write(byte[] b, int off, int len) throws IOException {
165165
@Override
166166
public void flush() throws IOException {
167167
ensureOpen();
168-
in.set(inSeg, 0, 0);
168+
in.set(inSeg, 0);
169169
long remainingHint;
170170
do {
171171
remainingHint = drainOutput(ZSTD_E_FLUSH);
@@ -179,7 +179,7 @@ public void close() throws IOException {
179179
return;
180180
}
181181
try {
182-
in.set(inSeg, 0, 0);
182+
in.set(inSeg, 0);
183183
long remainingHint;
184184
do {
185185
remainingHint = drainOutput(ZSTD_E_END);
@@ -196,7 +196,7 @@ public void close() throws IOException {
196196
/// Runs one compressStream2 call and writes whatever it produced to `out`.
197197
/// Returns the zstd "remaining" hint (0 means the directive is fully flushed).
198198
private long drainOutput(int directive) throws IOException {
199-
outBuf.set(outSeg, outCap, 0);
199+
outBuf.set(outSeg, outCap);
200200
long remainingHint = NativeCall.checkReturnValue(() -> (long) Bindings.COMPRESS_STREAM2.invokeExact(
201201
cctx, outBuf.segment(), in.segment(), directive));
202202
int produced = Math.toIntExact(outBuf.pos());

zstd/src/main/java/io/github/dfa1/zstd/ZstdSkippableContent.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public record ZstdSkippableContent(byte[] content, int magicVariant) {
1616
/// @return `true` if `o` is a [ZstdSkippableContent] with equal content bytes and variant
1717
@Override
1818
public boolean equals(Object o) {
19-
return o instanceof ZstdSkippableContent other
20-
&& magicVariant == other.magicVariant
21-
&& Arrays.equals(content, other.content);
19+
return o instanceof ZstdSkippableContent(byte[] otherContent, int otherVariant)
20+
&& magicVariant == otherVariant
21+
&& Arrays.equals(content, otherContent);
2222
}
2323

2424
/// Hash code consistent with [#equals(Object)], derived from the content bytes
@@ -35,6 +35,7 @@ public int hashCode() {
3535
///
3636
/// @return a string with the content length and magic variant
3737
@Override
38+
@SuppressWarnings("NullableProblems") // toString never returns null; we just don't pull in JB @NotNull
3839
public String toString() {
3940
return "ZstdSkippableContent[content=" + content.length + " bytes, magicVariant=" + magicVariant + "]";
4041
}

zstd/src/main/java/io/github/dfa1/zstd/ZstdStreamBuffer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ MemorySegment segment() {
2626
return struct;
2727
}
2828

29-
void set(MemorySegment buffer, long size, long pos) {
29+
/// Points the buffer at `buffer` with the given size and a fresh position of 0.
30+
void set(MemorySegment buffer, long size) {
3031
struct.set(ADDRESS, OFF_PTR, buffer);
3132
struct.set(JAVA_LONG, OFF_SIZE, size);
32-
struct.set(JAVA_LONG, OFF_POS, pos);
33+
struct.set(JAVA_LONG, OFF_POS, 0L);
3334
}
3435

3536
long size() {

zstd/src/test/java/io/github/dfa1/zstd/ZstdFrameTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.junit.jupiter.api.Test;
66

77
import java.io.ByteArrayOutputStream;
8+
import java.lang.foreign.Arena;
9+
import java.lang.foreign.MemorySegment;
810
import java.nio.charset.StandardCharsets;
911
import java.util.ArrayList;
1012
import java.util.List;
@@ -188,4 +190,36 @@ private ZstdDictionary trainDict() {
188190
return ZstdDictionary.train(samples, 8 * 1024);
189191
}
190192
}
193+
194+
@Nested
195+
class SegmentOverloads {
196+
197+
@Test
198+
void mirrorTheByteArrayOverloadsForANativeFrame() {
199+
// Given a frame as both a byte[] and the same bytes in a native segment
200+
byte[] frame = Zstd.compress(PAYLOAD);
201+
try (Arena arena = Arena.ofConfined()) {
202+
MemorySegment seg = Zstd.copyIn(arena, frame);
203+
204+
// When inspected through the zero-copy MemorySegment overloads
205+
// Then each agrees with its byte[] counterpart
206+
assertThat(ZstdFrame.isZstdFrame(seg)).isEqualTo(ZstdFrame.isZstdFrame(frame));
207+
assertThat(ZstdFrame.compressedSize(seg)).isEqualTo(ZstdFrame.compressedSize(frame));
208+
assertThat(ZstdFrame.decompressedBound(seg)).isEqualTo(ZstdFrame.decompressedBound(frame));
209+
assertThat(ZstdFrame.dictId(seg)).isEqualTo(ZstdFrame.dictId(frame));
210+
}
211+
}
212+
213+
@Test
214+
void recognisesASkippableNativeFrame() {
215+
// Given a skippable frame in a native segment
216+
byte[] frame = ZstdFrame.writeSkippableFrame("meta".getBytes(StandardCharsets.UTF_8), 5);
217+
try (Arena arena = Arena.ofConfined()) {
218+
MemorySegment seg = Zstd.copyIn(arena, frame);
219+
220+
// When tested through the MemorySegment overload / Then it is skippable
221+
assertThat(ZstdFrame.isSkippableFrame(seg)).isTrue();
222+
}
223+
}
224+
}
191225
}

0 commit comments

Comments
 (0)