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      private static final boolean supportsWindowSizeAndMemLevel;
35  
36      static {
37          noJdkZlibDecoder = SystemPropertyUtil.getBoolean("io.netty.noJdkZlibDecoder",
38                  PlatformDependent.javaVersion() < 7);
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          supportsWindowSizeAndMemLevel = noJdkZlibDecoder || PlatformDependent.javaVersion() >= 7;
45      }
46  
47      /**
48       * Returns {@code true} if specify a custom window size and mem level is supported.
49       */
50      public static boolean isSupportingWindowSizeAndMemLevel() {
51          return supportsWindowSizeAndMemLevel;
52      }
53  
54      public static ZlibEncoder newZlibEncoder(int compressionLevel) {
55          if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
56              return new JZlibEncoder(compressionLevel);
57          } else {
58              return new JdkZlibEncoder(compressionLevel);
59          }
60      }
61  
62      public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper) {
63          if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
64              return new JZlibEncoder(wrapper);
65          } else {
66              return new JdkZlibEncoder(wrapper);
67          }
68      }
69  
70      public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
71          if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
72              return new JZlibEncoder(wrapper, compressionLevel);
73          } else {
74              return new JdkZlibEncoder(wrapper, compressionLevel);
75          }
76      }
77  
78      public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, int memLevel) {
79          if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder ||
80                  windowBits != DEFAULT_JDK_WINDOW_SIZE || memLevel != DEFAULT_JDK_MEM_LEVEL) {
81              return new JZlibEncoder(wrapper, compressionLevel, windowBits, memLevel);
82          } else {
83              return new JdkZlibEncoder(wrapper, compressionLevel);
84          }
85      }
86  
87      public static ZlibEncoder newZlibEncoder(byte[] dictionary) {
88          if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
89              return new JZlibEncoder(dictionary);
90          } else {
91              return new JdkZlibEncoder(dictionary);
92          }
93      }
94  
95      public static ZlibEncoder newZlibEncoder(int compressionLevel, byte[] dictionary) {
96          if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
97              return new JZlibEncoder(compressionLevel, dictionary);
98          } else {
99              return new JdkZlibEncoder(compressionLevel, dictionary);
100         }
101     }
102 
103     public static ZlibEncoder newZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) {
104         if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder ||
105                 windowBits != DEFAULT_JDK_WINDOW_SIZE || memLevel != DEFAULT_JDK_MEM_LEVEL) {
106             return new JZlibEncoder(compressionLevel, windowBits, memLevel, dictionary);
107         } else {
108             return new JdkZlibEncoder(compressionLevel, dictionary);
109         }
110     }
111 
112     public static ZlibDecoder newZlibDecoder() {
113         if (PlatformDependent.javaVersion() < 7 || noJdkZlibDecoder) {
114             return new JZlibDecoder();
115         } else {
116             return new JdkZlibDecoder(true);
117         }
118     }
119 
120     public static ZlibDecoder newZlibDecoder(ZlibWrapper wrapper) {
121         if (PlatformDependent.javaVersion() < 7 || noJdkZlibDecoder) {
122             return new JZlibDecoder(wrapper);
123         } else {
124             return new JdkZlibDecoder(wrapper, true);
125         }
126     }
127 
128     public static ZlibDecoder newZlibDecoder(byte[] dictionary) {
129         if (PlatformDependent.javaVersion() < 7 || noJdkZlibDecoder) {
130             return new JZlibDecoder(dictionary);
131         } else {
132             return new JdkZlibDecoder(dictionary);
133         }
134     }
135 
136     private ZlibCodecFactory() {
137         // Unused
138     }
139 }