View Javadoc
1   /*
2    * Copyright 2021 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.compression;
17  
18  import io.netty.util.internal.ObjectUtil;
19  
20  import static io.netty.handler.codec.compression.ZstdConstants.DEFAULT_COMPRESSION_LEVEL;
21  import static io.netty.handler.codec.compression.ZstdConstants.MIN_COMPRESSION_LEVEL;
22  import static io.netty.handler.codec.compression.ZstdConstants.MAX_COMPRESSION_LEVEL;
23  import static io.netty.handler.codec.compression.ZstdConstants.DEFAULT_BLOCK_SIZE;
24  import static io.netty.handler.codec.compression.ZstdConstants.DEFAULT_MAX_ENCODE_SIZE;
25  
26  /**
27   * {@link ZstdOptions} holds compressionLevel for
28   * Zstd compression.
29   */
30  public class ZstdOptions implements CompressionOptions {
31  
32      private final int blockSize;
33      private final int compressionLevel;
34      private final int maxEncodeSize;
35  
36      /**
37       * Default implementation of {@link ZstdOptions} with{compressionLevel(int)} set to
38       * {@link ZstdConstants#DEFAULT_COMPRESSION_LEVEL},{@link ZstdConstants#DEFAULT_BLOCK_SIZE},
39       * {@link ZstdConstants#DEFAULT_MAX_ENCODE_SIZE}
40       */
41      static final ZstdOptions DEFAULT = new ZstdOptions(DEFAULT_COMPRESSION_LEVEL, DEFAULT_BLOCK_SIZE,
42              DEFAULT_MAX_ENCODE_SIZE);
43  
44      /**
45       * Create a new {@link ZstdOptions}
46       *
47       * @param  blockSize
48       *           is used to calculate the compressionLevel
49       * @param  maxEncodeSize
50       *           specifies the size of the largest compressed object
51       * @param  compressionLevel
52       *           specifies the level of the compression
53       */
54      ZstdOptions(int compressionLevel, int blockSize, int maxEncodeSize) {
55          if (!Zstd.isAvailable()) {
56              throw new IllegalStateException("zstd-jni is not available", Zstd.cause());
57          }
58  
59          this.compressionLevel = ObjectUtil.checkInRange(compressionLevel,
60                  MIN_COMPRESSION_LEVEL, MAX_COMPRESSION_LEVEL, "compressionLevel");
61          this.blockSize = ObjectUtil.checkPositive(blockSize, "blockSize");
62          this.maxEncodeSize = ObjectUtil.checkPositive(maxEncodeSize, "maxEncodeSize");
63      }
64  
65      public int compressionLevel() {
66          return compressionLevel;
67      }
68  
69      public int blockSize() {
70          return blockSize;
71      }
72  
73      public int maxEncodeSize() {
74          return maxEncodeSize;
75      }
76  }