From 9f4e155e3679b781e6073401045c382afb04358c Mon Sep 17 00:00:00 2001 From: skyguard1 Date: Thu, 19 Aug 2021 17:10:09 +0800 Subject: [PATCH] Add Zstd.isAvailable() check in ZstdOptions (#11597) Motivation: At present, the verification methods of `ZstdOptions` and `BrotliOptions` are not consistent, and the processing methods of `ZstdOptions` and `BrotliOptions` in `HttpContentCompressor` are also inconsistent. The http2 module does not add zstd-jni dependency, so `ClassNotFoundException` may be thrown Modification: Added `Zstd.isAvailable()` check in `ZstdOptions` to be consistent, and added zstd-jni dependency in http2 module Result: The verification methods of `ZstdOptions` and `BrotliOptions` are consistent, and `ClassNotFoundException` will not be thrown Signed-off-by: xingrufei --- .../io/netty/handler/codec/http/HttpContentCompressor.java | 3 +-- codec-http2/pom.xml | 5 +++++ .../io/netty/handler/codec/compression/BrotliOptions.java | 4 ++-- .../java/io/netty/handler/codec/compression/ZstdOptions.java | 4 ++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java index f92b467e65..9fb30f768c 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentCompressor.java @@ -186,8 +186,7 @@ public class HttpContentCompressor extends HttpContentEncoder { } else if (compressionOption instanceof DeflateOptions) { deflateOptions = (DeflateOptions) compressionOption; } else if (compressionOption instanceof ZstdOptions) { - // zstd might not be available - zstdOptions = Zstd.isAvailable() ? (ZstdOptions) compressionOption : null; + zstdOptions = (ZstdOptions) compressionOption; } else { throw new IllegalArgumentException("Unsupported " + CompressionOptions.class.getSimpleName() + ": " + compressionOption); diff --git a/codec-http2/pom.xml b/codec-http2/pom.xml index 37eaa1a2ab..f61c43f42c 100644 --- a/codec-http2/pom.xml +++ b/codec-http2/pom.xml @@ -95,6 +95,11 @@ brotli4j true + + com.github.luben + zstd-jni + true + diff --git a/codec/src/main/java/io/netty/handler/codec/compression/BrotliOptions.java b/codec/src/main/java/io/netty/handler/codec/compression/BrotliOptions.java index b2a6934aa5..737f5d149c 100644 --- a/codec/src/main/java/io/netty/handler/codec/compression/BrotliOptions.java +++ b/codec/src/main/java/io/netty/handler/codec/compression/BrotliOptions.java @@ -34,11 +34,11 @@ public final class BrotliOptions implements CompressionOptions { ); BrotliOptions(Encoder.Parameters parameters) { - this.parameters = ObjectUtil.checkNotNull(parameters, "Parameters"); - if (!Brotli.isAvailable()) { throw new IllegalStateException("Brotli is not available", Brotli.cause()); } + + this.parameters = ObjectUtil.checkNotNull(parameters, "Parameters"); } public Encoder.Parameters parameters() { diff --git a/codec/src/main/java/io/netty/handler/codec/compression/ZstdOptions.java b/codec/src/main/java/io/netty/handler/codec/compression/ZstdOptions.java index 3c8c243a55..110a90fb39 100644 --- a/codec/src/main/java/io/netty/handler/codec/compression/ZstdOptions.java +++ b/codec/src/main/java/io/netty/handler/codec/compression/ZstdOptions.java @@ -50,6 +50,10 @@ public class ZstdOptions implements CompressionOptions { * specifies the level of the compression */ ZstdOptions(int compressionLevel, int blockSize, int maxEncodeSize) { + if (!Zstd.isAvailable()) { + throw new IllegalStateException("zstd-jni is not available", Zstd.cause()); + } + this.compressionLevel = ObjectUtil.checkInRange(compressionLevel, 0, MAX_COMPRESSION_LEVEL, "compressionLevel"); this.blockSize = ObjectUtil.checkPositive(blockSize, "blockSize"); this.maxEncodeSize = ObjectUtil.checkPositive(maxEncodeSize, "maxEncodeSize");