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 org.jboss.netty.handler.codec.compression;
17  
18  import org.jboss.netty.util.internal.jzlib.JZlib;
19  import org.jboss.netty.util.internal.jzlib.ZStream;
20  
21  /**
22   * Utility methods used by {@link ZlibEncoder} and {@link ZlibDecoder}.
23   */
24  final class ZlibUtil {
25  
26      static void fail(ZStream z, String message, int resultCode) {
27          throw exception(z, message, resultCode);
28      }
29  
30      static CompressionException exception(ZStream z, String message, int resultCode) {
31          return new CompressionException(message + " (" + resultCode + ')' +
32                  (z.msg != null? ": " + z.msg : ""));
33      }
34  
35      static Enum<?> convertWrapperType(ZlibWrapper wrapper) {
36          Enum<?> convertedWrapperType;
37          switch (wrapper) {
38          case NONE:
39              convertedWrapperType = JZlib.W_NONE;
40              break;
41          case ZLIB:
42              convertedWrapperType = JZlib.W_ZLIB;
43              break;
44          case GZIP:
45              convertedWrapperType = JZlib.W_GZIP;
46              break;
47          case ZLIB_OR_NONE:
48              convertedWrapperType = JZlib.W_ZLIB_OR_NONE;
49              break;
50          default:
51              throw new Error();
52          }
53          return convertedWrapperType;
54      }
55  
56      static int wrapperOverhead(ZlibWrapper wrapper) {
57          int overhead;
58          switch (wrapper) {
59          case NONE:
60              overhead = 0;
61              break;
62          case ZLIB:
63          case ZLIB_OR_NONE:
64              overhead = 2;
65              break;
66          case GZIP:
67              overhead = 10;
68              break;
69          default:
70              throw new Error();
71          }
72          return overhead;
73      }
74  
75      private ZlibUtil() {
76      }
77  }