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.netty5.handler.codec.compression;
17  
18  import io.netty5.util.internal.ObjectUtil;
19  
20  import static io.netty5.handler.codec.compression.ZstdConstants.DEFAULT_BLOCK_SIZE;
21  import static io.netty5.handler.codec.compression.ZstdConstants.DEFAULT_COMPRESSION_LEVEL;
22  import static io.netty5.handler.codec.compression.ZstdConstants.MAX_BLOCK_SIZE;
23  import static io.netty5.handler.codec.compression.ZstdConstants.MAX_COMPRESSION_LEVEL;
24  
25  /**
26   * {@link ZstdOptions} holds compressionLevel for
27   * Zstd compression.
28   */
29  public class ZstdOptions implements CompressionOptions {
30  
31      private final int blockSize;
32      private final int compressionLevel;
33      private final int maxEncodeSize;
34  
35      /**
36       * Default implementation of {@link ZstdOptions} with{compressionLevel(int)} set to
37       * {@link ZstdConstants#DEFAULT_COMPRESSION_LEVEL},{@link ZstdConstants#DEFAULT_BLOCK_SIZE},
38       * {@link ZstdConstants#MAX_BLOCK_SIZE}
39       */
40      static final ZstdOptions DEFAULT = new ZstdOptions(DEFAULT_COMPRESSION_LEVEL, DEFAULT_BLOCK_SIZE, MAX_BLOCK_SIZE);
41  
42      /**
43       * Create a new {@link ZstdOptions}
44       *
45       * @param  blockSize
46       *           is used to calculate the compressionLevel
47       * @param  maxEncodeSize
48       *           specifies the size of the largest compressed object
49       * @param  compressionLevel
50       *           specifies the level of the compression
51       */
52      ZstdOptions(int compressionLevel, int blockSize, int maxEncodeSize) {
53          if (!Zstd.isAvailable()) {
54              throw new IllegalStateException("zstd-jni is not available", Zstd.cause());
55          }
56  
57          this.compressionLevel = ObjectUtil.checkInRange(compressionLevel, 0, MAX_COMPRESSION_LEVEL, "compressionLevel");
58          this.blockSize = ObjectUtil.checkPositive(blockSize, "blockSize");
59          this.maxEncodeSize = ObjectUtil.checkPositive(maxEncodeSize, "maxEncodeSize");
60      }
61  
62      public int compressionLevel() {
63          return compressionLevel;
64      }
65  
66      public int blockSize() {
67          return blockSize;
68      }
69  
70      public int maxEncodeSize() {
71          return maxEncodeSize;
72      }
73  }