Skip to content

Commit cc82bec

Browse files
committed
Add missing checks
1 parent 64332b7 commit cc82bec

File tree

7 files changed

+37
-22
lines changed

7 files changed

+37
-22
lines changed

api/src/main/java/dev/freya02/discord/zstd/api/DiscordZstdNativesLoader.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.freya02.discord.zstd.api;
22

3+
import dev.freya02.discord.zstd.internal.Checks;
34
import org.jspecify.annotations.NullMarked;
45

56
import java.io.IOException;
@@ -38,9 +39,7 @@ public static synchronized boolean isLoaded() {
3839
public static synchronized boolean load(Path path) {
3940
if (init)
4041
return false;
41-
//noinspection ConstantValue
42-
if (path == null)
43-
throw new IllegalArgumentException("path is null");
42+
Checks.notNull(path, "Path");
4443
if (!path.isAbsolute())
4544
throw new IllegalArgumentException("path is not absolute: " + path);
4645

@@ -71,12 +70,8 @@ public static synchronized boolean load(Path path) {
7170
public static synchronized boolean loadFromJar(String resourcePath, Class<?> clazz) throws IOException {
7271
if (init)
7372
return false;
74-
//noinspection ConstantValue
75-
if (resourcePath == null)
76-
throw new IllegalArgumentException("resourcePath is null");
77-
//noinspection ConstantValue
78-
if (clazz == null)
79-
throw new IllegalArgumentException("clazz is null");
73+
Checks.notNull(resourcePath, "Resource path");
74+
Checks.notNull(clazz, "Class");
8075

8176
Path nativePath = IOUtil.copyNativeFromJar(resourcePath, clazz);
8277
load(nativePath);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dev.freya02.discord.zstd.internal;
2+
3+
import org.jspecify.annotations.NullMarked;
4+
import org.jspecify.annotations.Nullable;
5+
6+
@NullMarked
7+
public class Checks {
8+
public static void notNull(@Nullable Object object, String name) {
9+
if (object == null) {
10+
throw new IllegalArgumentException(name + " cannot be null");
11+
}
12+
}
13+
}

ffm-impl/src/main/java/dev/freya02/discord/zstd/ffm/DiscordZstdFFMContext.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dev.freya02.discord.zstd.api.DiscordZstdContext;
44
import dev.freya02.discord.zstd.api.DiscordZstdException;
5+
import dev.freya02.discord.zstd.internal.Checks;
56
import org.jspecify.annotations.NullMarked;
67

78
import java.io.InputStream;
@@ -38,14 +39,13 @@ public void reset() {
3839

3940
@Override
4041
public InputStream createInputStream(byte[] input) {
42+
checkValid();
43+
Checks.notNull(input, "Input");
4144
return new DiscordZstdFFMInputStream(this, input);
4245
}
4346

4447
public void decompress(MemorySegment dst, long dstCapacity, MemorySegment dstPos, MemorySegment src, long srcSize, MemorySegment srcPos) {
45-
if (closed)
46-
throw new IllegalStateException("Context is closed");
47-
if (invalidated)
48-
throw new IllegalStateException("Context is in an errored state and needs to be reset");
48+
checkValid();
4949

5050
long result = Zstd.ZSTD_decompressStream_simpleArgs(stream, dst, dstCapacity, dstPos, src, srcSize, srcPos);
5151

@@ -58,4 +58,11 @@ public DiscordZstdException createException(String message) {
5858
invalidated = true;
5959
return new DiscordZstdException(message);
6060
}
61+
62+
private void checkValid() {
63+
if (closed)
64+
throw new IllegalStateException("Context is closed");
65+
if (invalidated)
66+
throw new IllegalStateException("Context is in an errored state and needs to be reset");
67+
}
6168
}

ffm-impl/src/main/java/dev/freya02/discord/zstd/ffm/DiscordZstdFFMDecompressor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dev.freya02.discord.zstd.api.DiscordZstdException;
44
import dev.freya02.discord.zstd.internal.AbstractZstdDecompressor;
5+
import dev.freya02.discord.zstd.internal.Checks;
56
import org.jspecify.annotations.NullMarked;
67
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
@@ -76,9 +77,7 @@ public byte[] decompress(byte[] data)
7677
throw new IllegalStateException("Decompressor is closed");
7778
if (invalidated)
7879
throw new IllegalStateException("Decompressor is in an errored state and needs to be reset");
79-
//noinspection ConstantValue
80-
if (data == null)
81-
throw new IllegalArgumentException("data is null");
80+
Checks.notNull(data, "Data");
8281

8382
if (LOG.isTraceEnabled())
8483
LOG.trace("Decompressing data {}", Arrays.toString(data));

jna-impl/src/main/java/dev/freya02/discord/zstd/jna/ZstdJNADecompressor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.sun.jna.Pointer;
44
import dev.freya02.discord.zstd.api.DiscordZstdException;
55
import dev.freya02.discord.zstd.internal.AbstractZstdDecompressor;
6+
import dev.freya02.discord.zstd.internal.Checks;
67
import org.jspecify.annotations.NullMarked;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
@@ -68,9 +69,7 @@ public byte[] decompress(byte[] data)
6869
throw new IllegalStateException("Decompressor is closed");
6970
if (invalidated)
7071
throw new IllegalStateException("Decompressor is in an errored state and needs to be reset");
71-
//noinspection ConstantValue
72-
if (data == null)
73-
throw new IllegalArgumentException("data is null");
72+
Checks.notNull(data, "Data");
7473

7574
if (LOG.isTraceEnabled())
7675
LOG.trace("Decompressing data {}", Arrays.toString(data));

jni-impl/src/main/java/dev/freya02/discord/zstd/jni/DiscordZstdJNIContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.freya02.discord.zstd.jni;
22

33
import dev.freya02.discord.zstd.api.DiscordZstdContext;
4+
import dev.freya02.discord.zstd.internal.Checks;
45
import org.jspecify.annotations.NullMarked;
56

67
import java.io.InputStream;
@@ -47,6 +48,8 @@ public void reset() {
4748

4849
@Override
4950
public InputStream createInputStream(byte[] input) {
51+
checkValid();
52+
Checks.notNull(input, "Input");
5053
return new DiscordZstdJNIInputStream(this, zds, input);
5154
}
5255

jni-impl/src/main/java/dev/freya02/discord/zstd/jni/DiscordZstdJNIDecompressor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dev.freya02.discord.zstd.api.DiscordZstdException;
44
import dev.freya02.discord.zstd.internal.AbstractZstdDecompressor;
5+
import dev.freya02.discord.zstd.internal.Checks;
56
import org.jspecify.annotations.NullMarked;
67
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
@@ -59,9 +60,7 @@ public byte[] decompress(byte[] data)
5960
throw new IllegalStateException("Decompressor is closed");
6061
if (invalidated)
6162
throw new IllegalStateException("Decompressor is in an errored state and needs to be reset");
62-
//noinspection ConstantValue
63-
if (data == null)
64-
throw new IllegalArgumentException("data is null");
63+
Checks.notNull(data, "Data");
6564

6665
if (LOG.isTraceEnabled())
6766
LOG.trace("Decompressing data {}", Arrays.toString(data));

0 commit comments

Comments
 (0)