View Javadoc

1   /*
2    * Copyright (c) 2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.eclipse.jetty.spdy;
18  
19  import java.util.zip.DataFormatException;
20  import java.util.zip.Deflater;
21  import java.util.zip.Inflater;
22  import java.util.zip.ZipException;
23  
24  public class StandardCompressionFactory implements CompressionFactory
25  {
26      @Override
27      public Compressor newCompressor()
28      {
29          return new StandardCompressor();
30      }
31  
32      @Override
33      public Decompressor newDecompressor()
34      {
35          return new StandardDecompressor();
36      }
37  
38      public static class StandardCompressor implements Compressor
39      {
40          private final Deflater deflater = new Deflater();
41  
42          @Override
43          public void setInput(byte[] input)
44          {
45              deflater.setInput(input);
46          }
47  
48          @Override
49          public void setDictionary(byte[] dictionary)
50          {
51              deflater.setDictionary(dictionary);
52          }
53  
54          @Override
55          public int compress(byte[] output)
56          {
57              return deflater.deflate(output, 0, output.length, Deflater.SYNC_FLUSH);
58          }
59      }
60  
61      public static class StandardDecompressor implements CompressionFactory.Decompressor
62      {
63          private final Inflater inflater = new Inflater();
64  
65          @Override
66          public void setDictionary(byte[] dictionary)
67          {
68              inflater.setDictionary(dictionary);
69          }
70  
71          @Override
72          public void setInput(byte[] input)
73          {
74              inflater.setInput(input);
75          }
76  
77          @Override
78          public int decompress(byte[] output) throws ZipException
79          {
80              try
81              {
82                  return inflater.inflate(output);
83              }
84              catch (DataFormatException x)
85              {
86                  throw (ZipException)new ZipException().initCause(x);
87              }
88          }
89      }
90  }