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.SystemPropertyUtil;
19  import io.netty.util.internal.logging.InternalLogger;
20  import io.netty.util.internal.logging.InternalLoggerFactory;
21  
22  /**
23   * Creates a new {@link ZlibEncoder} and a new {@link ZlibDecoder}.
24   */
25  public final class ZlibCodecFactory {
26      private static final InternalLogger logger = InternalLoggerFactory.getInstance(ZlibCodecFactory.class);
27  
28      private static final int DEFAULT_JDK_WINDOW_SIZE = 15;
29      private static final int DEFAULT_JDK_MEM_LEVEL = 8;
30  
31      private static final boolean noJdkZlibDecoder;
32      private static final boolean noJdkZlibEncoder;
33  
34      static {
35          noJdkZlibDecoder = SystemPropertyUtil.getBoolean("io.netty.noJdkZlibDecoder", false);
36          logger.debug("-Dio.netty.noJdkZlibDecoder: {}", noJdkZlibDecoder);
37  
38          noJdkZlibEncoder = SystemPropertyUtil.getBoolean("io.netty.noJdkZlibEncoder", false);
39          logger.debug("-Dio.netty.noJdkZlibEncoder: {}", noJdkZlibEncoder);
40      }
41  
42      /**
43       * Returns {@code true} if specify a custom window size and mem level is supported.
44       */
45      public static boolean isSupportingWindowSizeAndMemLevel() {
46          return true;
47      }
48  
49      public static ZlibEncoder newZlibEncoder(int compressionLevel) {
50          if (noJdkZlibEncoder) {
51              return new JZlibEncoder(compressionLevel);
52          } else {
53              return new JdkZlibEncoder(compressionLevel);
54          }
55      }
56  
57      public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper) {
58          if (noJdkZlibEncoder) {
59              return new JZlibEncoder(wrapper);
60          } else {
61              return new JdkZlibEncoder(wrapper);
62          }
63      }
64  
65      public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
66          if (noJdkZlibEncoder) {
67              return new JZlibEncoder(wrapper, compressionLevel);
68          } else {
69              return new JdkZlibEncoder(wrapper, compressionLevel);
70          }
71      }
72  
73      public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, int memLevel) {
74          if (noJdkZlibEncoder ||
75                  windowBits != DEFAULT_JDK_WINDOW_SIZE || memLevel != DEFAULT_JDK_MEM_LEVEL) {
76              return new JZlibEncoder(wrapper, compressionLevel, windowBits, memLevel);
77          } else {
78              return new JdkZlibEncoder(wrapper, compressionLevel);
79          }
80      }
81  
82      public static ZlibEncoder newZlibEncoder(byte[] dictionary) {
83          if (noJdkZlibEncoder) {
84              return new JZlibEncoder(dictionary);
85          } else {
86              return new JdkZlibEncoder(dictionary);
87          }
88      }
89  
90      public static ZlibEncoder newZlibEncoder(int compressionLevel, byte[] dictionary) {
91          if (noJdkZlibEncoder) {
92              return new JZlibEncoder(compressionLevel, dictionary);
93          } else {
94              return new JdkZlibEncoder(compressionLevel, dictionary);
95          }
96      }
97  
98      public static ZlibEncoder newZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) {
99          if (noJdkZlibEncoder ||
100                 windowBits != DEFAULT_JDK_WINDOW_SIZE || memLevel != DEFAULT_JDK_MEM_LEVEL) {
101             return new JZlibEncoder(compressionLevel, windowBits, memLevel, dictionary);
102         } else {
103             return new JdkZlibEncoder(compressionLevel, dictionary);
104         }
105     }
106 
107     public static ZlibDecoder newZlibDecoder() {
108         if (noJdkZlibDecoder) {
109             return new JZlibDecoder();
110         } else {
111             return new JdkZlibDecoder(true);
112         }
113     }
114 
115     public static ZlibDecoder newZlibDecoder(ZlibWrapper wrapper) {
116         if (noJdkZlibDecoder) {
117             return new JZlibDecoder(wrapper);
118         } else {
119             return new JdkZlibDecoder(wrapper, true);
120         }
121     }
122 
123     public static ZlibDecoder newZlibDecoder(byte[] dictionary) {
124         if (noJdkZlibDecoder) {
125             return new JZlibDecoder(dictionary);
126         } else {
127             return new JdkZlibDecoder(dictionary);
128         }
129     }
130 
131     private ZlibCodecFactory() {
132         // Unused
133     }
134 }