Skip to content

Commit 01e14bc

Browse files
authored
Provides Brotli settings without com.aayushatharva.brotli4j dependency (#14629)
Motivation: Currently, in Netty, it is difficult to configure **Brotli** compression parameters such as `quality`, `window`, and `mode` without adding the `com.aayushatharva.brotli4j` dependency. Modification: Add new enum to allow configure Brotli without the need to use brotlij classes. Result: Cleanup API.
1 parent d5bad42 commit 01e14bc

File tree

2 files changed

+88
-12
lines changed

2 files changed

+88
-12
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2024 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+
import static com.aayushatharva.brotli4j.encoder.Encoder.Mode;
21+
22+
/**
23+
* Provides a way to specify the Brotli compression mode.
24+
*/
25+
public enum BrotliMode {
26+
27+
/**
28+
* The compressor does not make any assumptions about the input data's properties,
29+
* making it suitable for a wide range of data types.
30+
* default mode.
31+
*/
32+
GENERIC,
33+
34+
/**
35+
* Optimized for UTF-8 formatted text input.
36+
*/
37+
TEXT,
38+
39+
/**
40+
* Designed specifically for font data compression, as used in WOFF 2.0.
41+
*/
42+
FONT;
43+
44+
/**
45+
* Convert to Brotli {@link Encoder.Mode}.
46+
*
47+
* @return a new {@link Encoder.Mode}
48+
*/
49+
Mode adapt() {
50+
switch (this) {
51+
case GENERIC:
52+
return Mode.GENERIC;
53+
case TEXT:
54+
return Mode.TEXT;
55+
case FONT:
56+
return Mode.FONT;
57+
default:
58+
throw new IllegalStateException("Unsupported enum value: " + this);
59+
}
60+
}
61+
}

codec/src/main/java/io/netty/handler/codec/compression/StandardCompressionOptions.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.netty.handler.codec.compression;
1717

1818
import com.aayushatharva.brotli4j.encoder.Encoder;
19+
import io.netty.util.internal.ObjectUtil;
1920

2021
/**
2122
* Standard Compression Options for {@link BrotliOptions},
@@ -40,11 +41,33 @@ public static BrotliOptions brotli() {
4041
*
4142
* @param parameters {@link Encoder.Parameters} Instance
4243
* @throws NullPointerException If {@link Encoder.Parameters} is {@code null}
44+
* @deprecated Use {@link #brotli(int, int, BrotliMode)}
4345
*/
46+
@Deprecated
4447
public static BrotliOptions brotli(Encoder.Parameters parameters) {
4548
return new BrotliOptions(parameters);
4649
}
4750

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+
4871
/**
4972
* Default implementation of {@link ZstdOptions} with{compressionLevel(int)} set to
5073
* {@link ZstdConstants#DEFAULT_COMPRESSION_LEVEL},{@link ZstdConstants#DEFAULT_BLOCK_SIZE},
@@ -57,26 +80,22 @@ public static ZstdOptions zstd() {
5780
/**
5881
* Create a new {@link ZstdOptions}
5982
*
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
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
6686
*/
6787
public static ZstdOptions zstd(int compressionLevel, int blockSize, int maxEncodeSize) {
6888
return new ZstdOptions(compressionLevel, blockSize, maxEncodeSize);
6989
}
7090

7191
/**
7292
* Create a new {@link SnappyOptions}
73-
*
7493
*/
7594
public static SnappyOptions snappy() {
7695
return new SnappyOptions();
7796
}
7897

79-
/**
98+
/**
8099
* Default implementation of {@link GzipOptions} with
81100
* {@code compressionLevel()} set to 6, {@code windowBits()} set to 15 and {@code memLevel()} set to 8.
82101
*/
@@ -90,12 +109,10 @@ public static GzipOptions gzip() {
90109
* @param compressionLevel {@code 1} yields the fastest compression and {@code 9} yields the
91110
* best compression. {@code 0} means no compression. The default
92111
* compression level is {@code 6}.
93-
*
94112
* @param windowBits The base two logarithm of the size of the history buffer. The
95113
* value should be in the range {@code 9} to {@code 15} inclusive.
96114
* Larger values result in better compression at the expense of
97115
* memory usage. The default value is {@code 15}.
98-
*
99116
* @param memLevel How much memory should be allocated for the internal compression
100117
* state. {@code 1} uses minimum memory and {@code 9} uses maximum
101118
* memory. Larger values result in better and faster compression
@@ -119,12 +136,10 @@ public static DeflateOptions deflate() {
119136
* @param compressionLevel {@code 1} yields the fastest compression and {@code 9} yields the
120137
* best compression. {@code 0} means no compression. The default
121138
* compression level is {@code 6}.
122-
*
123139
* @param windowBits The base two logarithm of the size of the history buffer. The
124140
* value should be in the range {@code 9} to {@code 15} inclusive.
125141
* Larger values result in better compression at the expense of
126142
* memory usage. The default value is {@code 15}.
127-
*
128143
* @param memLevel How much memory should be allocated for the internal compression
129144
* state. {@code 1} uses minimum memory and {@code 9} uses maximum
130145
* memory. Larger values result in better and faster compression

0 commit comments

Comments
 (0)