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 <xingrufei@sogou-inc.com>
This commit is contained in:
skyguard1 2021-08-19 17:10:09 +08:00 committed by GitHub
parent 93484071d6
commit 9f4e155e36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 4 deletions

View File

@ -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);

View File

@ -95,6 +95,11 @@
<artifactId>brotli4j</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -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() {

View File

@ -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");