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",
39                  PlatformDependent.javaVersion() < 7);
40          logger.debug("-Dio.netty.noJdkZlibDecoder: {}", noJdkZlibDecoder);
41  
42          noJdkZlibEncoder = SystemPropertyUtil.getBoolean("io.netty.noJdkZlibEncoder", false);
43          logger.debug("-Dio.netty.noJdkZlibEncoder: {}", noJdkZlibEncoder);
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 (PlatformDependent.javaVersion() < 7 || 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 (PlatformDependent.javaVersion() < 7 || 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 (PlatformDependent.javaVersion() < 7 || 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 (PlatformDependent.javaVersion() < 7 || 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 (PlatformDependent.javaVersion() < 7 || 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 (PlatformDependent.javaVersion() < 7 || 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 (PlatformDependent.javaVersion() < 7 || 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     public static ZlibDecoder newZlibDecoder() {
124         if (PlatformDependent.javaVersion() < 7 || noJdkZlibDecoder) {
125             return new JZlibDecoder();
126         } else {
127             return new JdkZlibDecoder(true);
128         }
129     }
130 
131     public static ZlibDecoder newZlibDecoder(ZlibWrapper wrapper) {
132         if (PlatformDependent.javaVersion() < 7 || noJdkZlibDecoder) {
133             return new JZlibDecoder(wrapper);
134         } else {
135             return new JdkZlibDecoder(wrapper, true);
136         }
137     }
138 
139     public static ZlibDecoder newZlibDecoder(byte[] dictionary) {
140         if (PlatformDependent.javaVersion() < 7 || noJdkZlibDecoder) {
141             return new JZlibDecoder(dictionary);
142         } else {
143             return new JdkZlibDecoder(dictionary);
144         }
145     }
146 
147     private ZlibCodecFactory() {
148         // Unused
149     }
150 }