View Javadoc
1   /*
2    * Copyright 2012 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 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   * Creates a new {@link ZlibEncoder} and a new {@link ZlibDecoder}.
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       * Returns {@code true} if specify a custom window size and mem level is supported.
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      * Create a new decoder instance.
125      *
126      * @deprecated Use {@link ZlibCodecFactory#newZlibDecoder(int)}.
127      */
128     @Deprecated
129     public static ZlibDecoder newZlibDecoder() {
130         return newZlibDecoder(0);
131     }
132 
133     /**
134      * Create a new decoder instance with specified maximum buffer allocation.
135      *
136      * @param maxAllocation
137      *           Maximum size of the decompression buffer. Must be >= 0.
138      *           If zero, maximum size is not limited by decoder.
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      * Create a new decoder instance with the specified wrapper.
150      *
151      * @deprecated Use {@link ZlibCodecFactory#newZlibDecoder(ZlibWrapper, int)}.
152      */
153     @Deprecated
154     public static ZlibDecoder newZlibDecoder(ZlibWrapper wrapper) {
155         return newZlibDecoder(wrapper, 0);
156     }
157 
158     /**
159      * Create a new decoder instance with the specified wrapper and maximum buffer allocation.
160      *
161      * @param maxAllocation
162      *          Maximum size of the decompression buffer. Must be >= 0.
163      *          If zero, maximum size is not limited by decoder.
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      * Create a new decoder instance with the specified preset dictionary. The wrapper
175      * is always {@link ZlibWrapper#ZLIB} because it is the only format that
176      * supports the preset dictionary.
177      *
178      * @deprecated Use {@link ZlibCodecFactory#newZlibDecoder(byte[], int)}.
179      */
180     @Deprecated
181     public static ZlibDecoder newZlibDecoder(byte[] dictionary) {
182         return newZlibDecoder(dictionary, 0);
183     }
184 
185     /**
186      * Create a new decoder instance with the specified preset dictionary and maximum buffer allocation.
187      * The wrapper is always {@link ZlibWrapper#ZLIB} because it is the only format that
188      * supports the preset dictionary.
189      *
190      * @param maxAllocation
191      *          Maximum size of the decompression buffer. Must be >= 0.
192      *          If zero, maximum size is not limited by decoder.
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         // Unused
204     }
205 }