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 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          JZlib.WrapperType convertedWrapperType;
45          switch (wrapper) {
46          case NONE:
47              convertedWrapperType = JZlib.W_NONE;
48              break;
49          case ZLIB:
50              convertedWrapperType = JZlib.W_ZLIB;
51              break;
52          case GZIP:
53              convertedWrapperType = JZlib.W_GZIP;
54              break;
55          case ZLIB_OR_NONE:
56              convertedWrapperType = JZlib.W_ANY;
57              break;
58          default:
59              throw new Error();
60          }
61          return convertedWrapperType;
62      }
63  
64      static int wrapperOverhead(ZlibWrapper wrapper) {
65          int overhead;
66          switch (wrapper) {
67          case NONE:
68              overhead = 0;
69              break;
70          case ZLIB:
71          case ZLIB_OR_NONE:
72              overhead = 2;
73              break;
74          case GZIP:
75              overhead = 10;
76              break;
77          default:
78              throw new Error();
79          }
80          return overhead;
81      }
82  
83      private ZlibUtil() {
84      }
85  }