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