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 com.jcraft.jzlib.Deflater;
19  import com.jcraft.jzlib.Inflater;
20  import com.jcraft.jzlib.JZlib;
21  
22  /**
23   * Utility methods used by {@link JZlibEncoder} and {@link JZlibDecoder}.
24   */
25  final class ZlibUtil {
26  
27      static void fail(Inflater z, String message, int resultCode) {
28          throw inflaterException(z, message, resultCode);
29      }
30  
31      static void fail(Deflater z, String message, int resultCode) {
32          throw deflaterException(z, message, resultCode);
33      }
34  
35      static DecompressionException inflaterException(Inflater z, String message, int resultCode) {
36          return new DecompressionException(message + " (" + resultCode + ')' + (z.msg != null? ": " + z.msg : ""));
37      }
38  
39      static CompressionException deflaterException(Deflater z, String message, int resultCode) {
40          return new CompressionException(message + " (" + resultCode + ')' + (z.msg != null? ": " + z.msg : ""));
41      }
42  
43      static JZlib.WrapperType convertWrapperType(ZlibWrapper wrapper) {
44          switch (wrapper) {
45          case NONE:
46              return JZlib.W_NONE;
47          case ZLIB:
48              return JZlib.W_ZLIB;
49          case GZIP:
50              return JZlib.W_GZIP;
51          case ZLIB_OR_NONE:
52              return JZlib.W_ANY;
53          default:
54              throw new Error("Unexpected wrapper type: " + wrapper);
55          }
56      }
57  
58      static int wrapperOverhead(ZlibWrapper wrapper) {
59          switch (wrapper) {
60          case NONE:
61              return 0;
62          case ZLIB:
63          case ZLIB_OR_NONE:
64              return 2;
65          case GZIP:
66              return 10;
67          default:
68              throw new Error("Unexpected wrapper type: " + wrapper);
69          }
70      }
71  
72      private ZlibUtil() {
73      }
74  }