View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.client.entity;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.PushbackInputStream;
32  import java.util.zip.Inflater;
33  import java.util.zip.InflaterInputStream;
34  import java.util.zip.ZipException;
35  
36  /**
37   * Deflate input stream.    This class includes logic needed for various Rfc's in order
38   * to reasonably implement the "deflate" compression style.
39   */
40  public class DeflateInputStream extends InputStream {
41  
42      private final InputStream sourceStream;
43  
44      public DeflateInputStream(final InputStream wrapped) throws IOException {
45  
46          final PushbackInputStream pushback = new PushbackInputStream(wrapped, 2);
47          final int i1 = pushback.read();
48          final int i2 = pushback.read();
49          if (i1 == -1 || i2 == -1) {
50              throw new ZipException("Unexpected end of stream");
51          }
52  
53          pushback.unread(i2);
54          pushback.unread(i1);
55  
56          boolean nowrap = true;
57          final int b1 = i1 & 0xFF;
58          final int compressionMethod = b1 & 0xF;
59          final int compressionInfo = b1 >> 4 & 0xF;
60          final int b2 = i2 & 0xFF;
61          if (compressionMethod == 8 && compressionInfo <= 7 && ((b1 << 8) | b2) % 31 == 0) {
62              nowrap = false;
63          }
64          sourceStream = new DeflateStream(pushback, new Inflater(nowrap));
65      }
66  
67      /**
68       * Read a byte.
69       */
70      @Override
71      public int read() throws IOException {
72          return sourceStream.read();
73      }
74  
75      /**
76       * Read lots of bytes.
77       */
78      @Override
79      public int read(final byte[] b) throws IOException {
80          return sourceStream.read(b);
81      }
82  
83      /**
84       * Read lots of specific bytes.
85       */
86      @Override
87      public int read(final byte[] b, final int off, final int len) throws IOException {
88          return sourceStream.read(b, off, len);
89      }
90  
91      /**
92       * Skip
93       */
94      @Override
95      public long skip(final long n) throws IOException {
96          return sourceStream.skip(n);
97      }
98  
99      /**
100      * Get available.
101      */
102     @Override
103     public int available() throws IOException {
104         return sourceStream.available();
105     }
106 
107     /**
108      * Mark.
109      */
110     @Override
111     public void mark(final int readLimit) {
112         sourceStream.mark(readLimit);
113     }
114 
115     /**
116      * Reset.
117      */
118     @Override
119     public void reset() throws IOException {
120         sourceStream.reset();
121     }
122 
123     /**
124      * Check if mark is supported.
125      */
126     @Override
127     public boolean markSupported() {
128         return sourceStream.markSupported();
129     }
130 
131     /**
132      * Close.
133      */
134     @Override
135     public void close() throws IOException {
136         sourceStream.close();
137     }
138 
139     static class DeflateStream extends InflaterInputStream {
140 
141         private boolean closed = false;
142 
143         public DeflateStream(final InputStream in, final Inflater inflater) {
144             super(in, inflater);
145         }
146 
147         @Override
148         public void close() throws IOException {
149             if (closed) {
150                 return;
151             }
152             closed = true;
153             inf.end();
154             super.close();
155         }
156 
157     }
158 
159 }
160