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.MAX_BLOCK_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#MAX_BLOCK_SIZE}
40       */
41      static final ZstdOptions DEFAULT = new ZstdOptions(DEFAULT_COMPRESSION_LEVEL, DEFAULT_BLOCK_SIZE, MAX_BLOCK_SIZE);
42  
43      /**
44       * Create a new {@link ZstdOptions}
45       *
46       * @param  blockSize
47       *           is used to calculate the compressionLevel
48       * @param  maxEncodeSize
49       *           specifies the size of the largest compressed object
50       * @param  compressionLevel
51       *           specifies the level of the compression
52       */
53      ZstdOptions(int compressionLevel, int blockSize, int maxEncodeSize) {
54          if (!Zstd.isAvailable()) {
55              throw new IllegalStateException("zstd-jni is not available", Zstd.cause());
56          }
57  
58          this.compressionLevel = ObjectUtil.checkInRange(compressionLevel,
59                  MIN_COMPRESSION_LEVEL, MAX_COMPRESSION_LEVEL, "compressionLevel");
60          this.blockSize = ObjectUtil.checkPositive(blockSize, "blockSize");
61          this.maxEncodeSize = ObjectUtil.checkPositive(maxEncodeSize, "maxEncodeSize");
62      }
63  
64      public int compressionLevel() {
65          return compressionLevel;
66      }
67  
68      public int blockSize() {
69          return blockSize;
70      }
71  
72      public int maxEncodeSize() {
73          return maxEncodeSize;
74      }
75  }