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 import io.netty.util.internal.ObjectUtil; 20 21 /** 22 * Standard Compression Options for {@link BrotliOptions}, 23 * {@link GzipOptions} and {@link DeflateOptions} 24 */ 25 public final class StandardCompressionOptions { 26 27 private StandardCompressionOptions() { 28 // Prevent outside initialization 29 } 30 31 /** 32 * Default implementation of {@link BrotliOptions} with {@link Encoder.Parameters#setQuality(int)} set to 4 33 * and {@link Encoder.Parameters#setMode(Encoder.Mode)} set to {@link Encoder.Mode#TEXT} 34 */ 35 public static BrotliOptions brotli() { 36 return BrotliOptions.DEFAULT; 37 } 38 39 /** 40 * Create a new {@link BrotliOptions} 41 * 42 * @param parameters {@link Encoder.Parameters} Instance 43 * @throws NullPointerException If {@link Encoder.Parameters} is {@code null} 44 * @deprecated Use {@link #brotli(int, int, BrotliMode)} 45 */ 46 @Deprecated 47 public static BrotliOptions brotli(Encoder.Parameters parameters) { 48 return new BrotliOptions(parameters); 49 } 50 51 /** 52 * Create a new {@link BrotliOptions} 53 * 54 * @param quality Specifies the compression level. 55 * @param window Specifies the size of the sliding window when compressing. 56 * @param mode optimizes the compression algorithm based on the type of input data. 57 * @throws NullPointerException If {@link BrotliMode} is {@code null} 58 */ 59 public static BrotliOptions brotli(int quality, int window, BrotliMode mode) { 60 ObjectUtil.checkInRange(quality, 0, 11, "quality"); 61 ObjectUtil.checkInRange(window, 10, 24, "window"); 62 ObjectUtil.checkNotNull(mode, "mode"); 63 64 Encoder.Parameters parameters = new Encoder.Parameters() 65 .setQuality(quality) 66 .setWindow(window) 67 .setMode(mode.adapt()); 68 return new BrotliOptions(parameters); 69 } 70 71 /** 72 * Default implementation of {@link ZstdOptions} with{compressionLevel(int)} set to 73 * {@link ZstdConstants#DEFAULT_COMPRESSION_LEVEL},{@link ZstdConstants#DEFAULT_BLOCK_SIZE}, 74 * {@link ZstdConstants#MAX_BLOCK_SIZE} 75 */ 76 public static ZstdOptions zstd() { 77 return ZstdOptions.DEFAULT; 78 } 79 80 /** 81 * Create a new {@link ZstdOptions} 82 * 83 * @param blockSize is used to calculate the compressionLevel 84 * @param maxEncodeSize specifies the size of the largest compressed object 85 * @param compressionLevel specifies the level of the compression 86 */ 87 public static ZstdOptions zstd(int compressionLevel, int blockSize, int maxEncodeSize) { 88 return new ZstdOptions(compressionLevel, blockSize, maxEncodeSize); 89 } 90 91 /** 92 * Create a new {@link SnappyOptions} 93 */ 94 public static SnappyOptions snappy() { 95 return new SnappyOptions(); 96 } 97 98 /** 99 * Default implementation of {@link GzipOptions} with 100 * {@code compressionLevel()} set to 6, {@code windowBits()} set to 15 and {@code memLevel()} set to 8. 101 */ 102 public static GzipOptions gzip() { 103 return GzipOptions.DEFAULT; 104 } 105 106 /** 107 * Create a new {@link GzipOptions} Instance 108 * 109 * @param compressionLevel {@code 1} yields the fastest compression and {@code 9} yields the 110 * best compression. {@code 0} means no compression. The default 111 * compression level is {@code 6}. 112 * @param windowBits The base two logarithm of the size of the history buffer. The 113 * value should be in the range {@code 9} to {@code 15} inclusive. 114 * Larger values result in better compression at the expense of 115 * memory usage. The default value is {@code 15}. 116 * @param memLevel How much memory should be allocated for the internal compression 117 * state. {@code 1} uses minimum memory and {@code 9} uses maximum 118 * memory. Larger values result in better and faster compression 119 * at the expense of memory usage. The default value is {@code 8} 120 */ 121 public static GzipOptions gzip(int compressionLevel, int windowBits, int memLevel) { 122 return new GzipOptions(compressionLevel, windowBits, memLevel); 123 } 124 125 /** 126 * Default implementation of {@link DeflateOptions} with 127 * {@code compressionLevel} set to 6, {@code windowBits} set to 15 and {@code memLevel} set to 8. 128 */ 129 public static DeflateOptions deflate() { 130 return DeflateOptions.DEFAULT; 131 } 132 133 /** 134 * Create a new {@link DeflateOptions} Instance 135 * 136 * @param compressionLevel {@code 1} yields the fastest compression and {@code 9} yields the 137 * best compression. {@code 0} means no compression. The default 138 * compression level is {@code 6}. 139 * @param windowBits The base two logarithm of the size of the history buffer. The 140 * value should be in the range {@code 9} to {@code 15} inclusive. 141 * Larger values result in better compression at the expense of 142 * memory usage. The default value is {@code 15}. 143 * @param memLevel How much memory should be allocated for the internal compression 144 * state. {@code 1} uses minimum memory and {@code 9} uses maximum 145 * memory. Larger values result in better and faster compression 146 * at the expense of memory usage. The default value is {@code 8} 147 */ 148 public static DeflateOptions deflate(int compressionLevel, int windowBits, int memLevel) { 149 return new DeflateOptions(compressionLevel, windowBits, memLevel); 150 } 151 }