1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.compression;
17
18 import io.netty.util.internal.PlatformDependent;
19 import io.netty.util.internal.SystemPropertyUtil;
20 import io.netty.util.internal.logging.InternalLogger;
21 import io.netty.util.internal.logging.InternalLoggerFactory;
22
23
24
25
26 public final class ZlibCodecFactory {
27 private static final InternalLogger logger = InternalLoggerFactory.getInstance(ZlibCodecFactory.class);
28
29 private static final int DEFAULT_JDK_WINDOW_SIZE = 15;
30 private static final int DEFAULT_JDK_MEM_LEVEL = 8;
31
32 private static final boolean noJdkZlibDecoder;
33 private static final boolean noJdkZlibEncoder;
34
35 private static final boolean JZLIB_AVAILABLE;
36
37 static {
38 noJdkZlibDecoder = SystemPropertyUtil.getBoolean("io.netty.noJdkZlibDecoder", false);
39 logger.debug("-Dio.netty.noJdkZlibDecoder: {}", noJdkZlibDecoder);
40
41 noJdkZlibEncoder = SystemPropertyUtil.getBoolean("io.netty.noJdkZlibEncoder", false);
42 logger.debug("-Dio.netty.noJdkZlibEncoder: {}", noJdkZlibEncoder);
43
44 boolean jzlibAvailable;
45 try {
46 Class.forName("com.jcraft.jzlib.JZlib", false,
47 PlatformDependent.getClassLoader(ZlibCodecFactory.class));
48 jzlibAvailable = true;
49 } catch (ClassNotFoundException t) {
50 jzlibAvailable = false;
51 logger.debug(
52 "JZlib not in the classpath; the only window bits supported value will be " +
53 DEFAULT_JDK_WINDOW_SIZE);
54 }
55 JZLIB_AVAILABLE = jzlibAvailable;
56 }
57
58
59
60
61 public static boolean isSupportingWindowSizeAndMemLevel() {
62 return JZLIB_AVAILABLE;
63 }
64
65 public static ZlibEncoder newZlibEncoder(int compressionLevel) {
66 if (noJdkZlibEncoder) {
67 return new JZlibEncoder(compressionLevel);
68 } else {
69 return new JdkZlibEncoder(compressionLevel);
70 }
71 }
72
73 public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper) {
74 if (noJdkZlibEncoder) {
75 return new JZlibEncoder(wrapper);
76 } else {
77 return new JdkZlibEncoder(wrapper);
78 }
79 }
80
81 public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
82 if (noJdkZlibEncoder) {
83 return new JZlibEncoder(wrapper, compressionLevel);
84 } else {
85 return new JdkZlibEncoder(wrapper, compressionLevel);
86 }
87 }
88
89 public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, int memLevel) {
90 if (noJdkZlibEncoder ||
91 windowBits != DEFAULT_JDK_WINDOW_SIZE || memLevel != DEFAULT_JDK_MEM_LEVEL) {
92 return new JZlibEncoder(wrapper, compressionLevel, windowBits, memLevel);
93 } else {
94 return new JdkZlibEncoder(wrapper, compressionLevel);
95 }
96 }
97
98 public static ZlibEncoder newZlibEncoder(byte[] dictionary) {
99 if (noJdkZlibEncoder) {
100 return new JZlibEncoder(dictionary);
101 } else {
102 return new JdkZlibEncoder(dictionary);
103 }
104 }
105
106 public static ZlibEncoder newZlibEncoder(int compressionLevel, byte[] dictionary) {
107 if (noJdkZlibEncoder) {
108 return new JZlibEncoder(compressionLevel, dictionary);
109 } else {
110 return new JdkZlibEncoder(compressionLevel, dictionary);
111 }
112 }
113
114 public static ZlibEncoder newZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) {
115 if (noJdkZlibEncoder ||
116 windowBits != DEFAULT_JDK_WINDOW_SIZE || memLevel != DEFAULT_JDK_MEM_LEVEL) {
117 return new JZlibEncoder(compressionLevel, windowBits, memLevel, dictionary);
118 } else {
119 return new JdkZlibEncoder(compressionLevel, dictionary);
120 }
121 }
122
123
124
125
126
127
128 @Deprecated
129 public static ZlibDecoder newZlibDecoder() {
130 return newZlibDecoder(0);
131 }
132
133
134
135
136
137
138
139
140 public static ZlibDecoder newZlibDecoder(int maxAllocation) {
141 if (noJdkZlibDecoder) {
142 return new JZlibDecoder(maxAllocation);
143 } else {
144 return new JdkZlibDecoder(true, maxAllocation);
145 }
146 }
147
148
149
150
151
152
153 @Deprecated
154 public static ZlibDecoder newZlibDecoder(ZlibWrapper wrapper) {
155 return newZlibDecoder(wrapper, 0);
156 }
157
158
159
160
161
162
163
164
165 public static ZlibDecoder newZlibDecoder(ZlibWrapper wrapper, int maxAllocation) {
166 if (noJdkZlibDecoder) {
167 return new JZlibDecoder(wrapper, maxAllocation);
168 } else {
169 return new JdkZlibDecoder(wrapper, true, maxAllocation);
170 }
171 }
172
173
174
175
176
177
178
179
180 @Deprecated
181 public static ZlibDecoder newZlibDecoder(byte[] dictionary) {
182 return newZlibDecoder(dictionary, 0);
183 }
184
185
186
187
188
189
190
191
192
193
194 public static ZlibDecoder newZlibDecoder(byte[] dictionary, int maxAllocation) {
195 if (noJdkZlibDecoder) {
196 return new JZlibDecoder(dictionary, maxAllocation);
197 } else {
198 return new JdkZlibDecoder(dictionary, maxAllocation);
199 }
200 }
201
202 private ZlibCodecFactory() {
203
204 }
205 }