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 com.aayushatharva.brotli4j.encoder.Encoder;
19  
20  /**
21   * Standard Compression Options for {@link BrotliOptions},
22   * {@link GzipOptions} and {@link DeflateOptions}
23   */
24  public final class StandardCompressionOptions {
25  
26      private StandardCompressionOptions() {
27          // Prevent outside initialization
28      }
29  
30      /**
31       * Default implementation of {@link BrotliOptions} with {@link Encoder.Parameters#setQuality(int)} set to 4
32       * and {@link Encoder.Parameters#setMode(Encoder.Mode)} set to {@link Encoder.Mode#TEXT}
33       */
34      public static BrotliOptions brotli() {
35          return BrotliOptions.DEFAULT;
36      }
37  
38      /**
39       * Create a new {@link BrotliOptions}
40       *
41       * @param parameters {@link Encoder.Parameters} Instance
42       * @throws NullPointerException If {@link Encoder.Parameters} is {@code null}
43       */
44      public static BrotliOptions brotli(Encoder.Parameters parameters) {
45          return new BrotliOptions(parameters);
46      }
47  
48      /**
49       * Default implementation of {@link ZstdOptions} with{compressionLevel(int)} set to
50       * {@link ZstdConstants#DEFAULT_COMPRESSION_LEVEL},{@link ZstdConstants#DEFAULT_BLOCK_SIZE},
51       * {@link ZstdConstants#MAX_BLOCK_SIZE}
52       */
53      public static ZstdOptions zstd() {
54          return ZstdOptions.DEFAULT;
55      }
56  
57      /**
58       * Create a new {@link ZstdOptions}
59       *
60       * @param  blockSize
61       *           is used to calculate the compressionLevel
62       * @param  maxEncodeSize
63       *           specifies the size of the largest compressed object
64       * @param  compressionLevel
65       *           specifies the level of the compression
66       */
67      public static ZstdOptions zstd(int compressionLevel, int blockSize, int maxEncodeSize) {
68          return new ZstdOptions(compressionLevel, blockSize, maxEncodeSize);
69      }
70  
71       /**
72       * Default implementation of {@link GzipOptions} with
73       * {@code compressionLevel()} set to 6, {@code windowBits()} set to 15 and {@code memLevel()} set to 8.
74       */
75      public static GzipOptions gzip() {
76          return GzipOptions.DEFAULT;
77      }
78  
79      /**
80       * Create a new {@link GzipOptions} Instance
81       *
82       * @param compressionLevel {@code 1} yields the fastest compression and {@code 9} yields the
83       *                         best compression.  {@code 0} means no compression.  The default
84       *                         compression level is {@code 6}.
85       *
86       * @param windowBits       The base two logarithm of the size of the history buffer.  The
87       *                         value should be in the range {@code 9} to {@code 15} inclusive.
88       *                         Larger values result in better compression at the expense of
89       *                         memory usage.  The default value is {@code 15}.
90       *
91       * @param memLevel         How much memory should be allocated for the internal compression
92       *                         state.  {@code 1} uses minimum memory and {@code 9} uses maximum
93       *                         memory.  Larger values result in better and faster compression
94       *                         at the expense of memory usage.  The default value is {@code 8}
95       */
96      public static GzipOptions gzip(int compressionLevel, int windowBits, int memLevel) {
97          return new GzipOptions(compressionLevel, windowBits, memLevel);
98      }
99  
100     /**
101      * Default implementation of {@link DeflateOptions} with
102      * {@code compressionLevel} set to 6, {@code windowBits} set to 15 and {@code memLevel} set to 8.
103      */
104     public static DeflateOptions deflate() {
105         return DeflateOptions.DEFAULT;
106     }
107 
108     /**
109      * Create a new {@link DeflateOptions} Instance
110      *
111      * @param compressionLevel {@code 1} yields the fastest compression and {@code 9} yields the
112      *                         best compression.  {@code 0} means no compression.  The default
113      *                         compression level is {@code 6}.
114      *
115      * @param windowBits       The base two logarithm of the size of the history buffer.  The
116      *                         value should be in the range {@code 9} to {@code 15} inclusive.
117      *                         Larger values result in better compression at the expense of
118      *                         memory usage.  The default value is {@code 15}.
119      *
120      * @param memLevel         How much memory should be allocated for the internal compression
121      *                         state.  {@code 1} uses minimum memory and {@code 9} uses maximum
122      *                         memory.  Larger values result in better and faster compression
123      *                         at the expense of memory usage.  The default value is {@code 8}
124      */
125     public static DeflateOptions deflate(int compressionLevel, int windowBits, int memLevel) {
126         return new DeflateOptions(compressionLevel, windowBits, memLevel);
127     }
128 }