View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.spdy;
20  
21  import java.util.zip.DataFormatException;
22  import java.util.zip.Deflater;
23  import java.util.zip.Inflater;
24  import java.util.zip.ZipException;
25  
26  public class StandardCompressionFactory implements CompressionFactory
27  {
28      @Override
29      public Compressor newCompressor()
30      {
31          return new StandardCompressor();
32      }
33  
34      @Override
35      public Decompressor newDecompressor()
36      {
37          return new StandardDecompressor();
38      }
39  
40      public static class StandardCompressor implements Compressor
41      {
42          private final Deflater deflater = new Deflater();
43  
44          @Override
45          public void setInput(byte[] input)
46          {
47              deflater.setInput(input);
48          }
49  
50          @Override
51          public void setDictionary(byte[] dictionary)
52          {
53              deflater.setDictionary(dictionary);
54          }
55  
56          @Override
57          public int compress(byte[] output)
58          {
59              return deflater.deflate(output, 0, output.length, Deflater.SYNC_FLUSH);
60          }
61      }
62  
63      public static class StandardDecompressor implements CompressionFactory.Decompressor
64      {
65          private final Inflater inflater = new Inflater();
66  
67          @Override
68          public void setDictionary(byte[] dictionary)
69          {
70              inflater.setDictionary(dictionary);
71          }
72  
73          @Override
74          public void setInput(byte[] input)
75          {
76              inflater.setInput(input);
77          }
78  
79          @Override
80          public int decompress(byte[] output) throws ZipException
81          {
82              try
83              {
84                  return inflater.inflate(output);
85              }
86              catch (DataFormatException x)
87              {
88                  throw (ZipException)new ZipException().initCause(x);
89              }
90          }
91      }
92  }