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    *   http://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      static {
36          noJdkZlibDecoder = SystemPropertyUtil.getBoolean("io.netty.noJdkZlibDecoder", true);
37          logger.debug("-Dio.netty.noJdkZlibDecoder: {}", noJdkZlibDecoder);
38  
39          noJdkZlibEncoder = SystemPropertyUtil.getBoolean("io.netty.noJdkZlibEncoder", false);
40          logger.debug("-Dio.netty.noJdkZlibEncoder: {}", noJdkZlibEncoder);
41      }
42  
43      public static ZlibEncoder newZlibEncoder(int compressionLevel) {
44          if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
45              return new JZlibEncoder(compressionLevel);
46          } else {
47              return new JdkZlibEncoder(compressionLevel);
48          }
49      }
50  
51      public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper) {
52          if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
53              return new JZlibEncoder(wrapper);
54          } else {
55              return new JdkZlibEncoder(wrapper);
56          }
57      }
58  
59      public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
60          if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
61              return new JZlibEncoder(wrapper, compressionLevel);
62          } else {
63              return new JdkZlibEncoder(wrapper, compressionLevel);
64          }
65      }
66  
67      public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, int memLevel) {
68          if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder ||
69              windowBits != DEFAULT_JDK_WINDOW_SIZE || memLevel != DEFAULT_JDK_MEM_LEVEL) {
70              return new JZlibEncoder(wrapper, compressionLevel, windowBits, memLevel);
71          } else {
72              return new JdkZlibEncoder(wrapper, compressionLevel);
73          }
74      }
75  
76      public static ZlibEncoder newZlibEncoder(byte[] dictionary) {
77          if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
78              return new JZlibEncoder(dictionary);
79          } else {
80              return new JdkZlibEncoder(dictionary);
81          }
82      }
83  
84      public static ZlibEncoder newZlibEncoder(int compressionLevel, byte[] dictionary) {
85          if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder) {
86              return new JZlibEncoder(compressionLevel, dictionary);
87          } else {
88              return new JdkZlibEncoder(compressionLevel, dictionary);
89          }
90      }
91  
92      public static ZlibEncoder newZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) {
93          if (PlatformDependent.javaVersion() < 7 || noJdkZlibEncoder ||
94              windowBits != DEFAULT_JDK_WINDOW_SIZE || memLevel != DEFAULT_JDK_MEM_LEVEL) {
95              return new JZlibEncoder(compressionLevel, windowBits, memLevel, dictionary);
96          } else {
97              return new JdkZlibEncoder(compressionLevel, dictionary);
98          }
99      }
100 
101     public static ZlibDecoder newZlibDecoder() {
102         if (PlatformDependent.javaVersion() < 7 || noJdkZlibDecoder) {
103             return new JZlibDecoder();
104         } else {
105             return new JdkZlibDecoder();
106         }
107     }
108 
109     public static ZlibDecoder newZlibDecoder(ZlibWrapper wrapper) {
110         if (PlatformDependent.javaVersion() < 7 || noJdkZlibDecoder) {
111             return new JZlibDecoder(wrapper);
112         } else {
113             return new JdkZlibDecoder(wrapper);
114         }
115     }
116 
117     public static ZlibDecoder newZlibDecoder(byte[] dictionary) {
118         if (PlatformDependent.javaVersion() < 7 || noJdkZlibDecoder) {
119             return new JZlibDecoder(dictionary);
120         } else {
121             return new JdkZlibDecoder(dictionary);
122         }
123     }
124 
125     private ZlibCodecFactory() {
126         // Unused
127     }
128 }