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  
32  import org.apache.http.annotation.NotThreadSafe;
33  
34  /**
35   * Lazy init InputStream wrapper.
36   */
37  @NotThreadSafe
38  class LazyDecompressingInputStream extends InputStream {
39  
40      private final InputStream wrappedStream;
41      private final InputStreamFactory inputStreamFactory;
42  
43      private InputStream wrapperStream;
44  
45      public LazyDecompressingInputStream(
46              final InputStream wrappedStream,
47              final InputStreamFactory inputStreamFactory) {
48          this.wrappedStream = wrappedStream;
49          this.inputStreamFactory = inputStreamFactory;
50      }
51  
52      private void initWrapper() throws IOException {
53          if (wrapperStream == null) {
54              wrapperStream = inputStreamFactory.create(wrappedStream);
55          }
56      }
57  
58      @Override
59      public int read() throws IOException {
60          initWrapper();
61          return wrapperStream.read();
62      }
63  
64      @Override
65      public int read(final byte[] b) throws IOException {
66          initWrapper();
67          return wrapperStream.read(b);
68      }
69  
70      @Override
71      public int read(final byte[] b, final int off, final int len) throws IOException {
72          initWrapper();
73          return wrapperStream.read(b, off, len);
74      }
75  
76      @Override
77      public long skip(final long n) throws IOException {
78          initWrapper();
79          return wrapperStream.skip(n);
80      }
81  
82      @Override
83      public boolean markSupported() {
84          return false;
85      }
86  
87      @Override
88      public int available() throws IOException {
89          initWrapper();
90          return wrapperStream.available();
91      }
92  
93      @Override
94      public void close() throws IOException {
95          try {
96              if (wrapperStream != null) {
97                  wrapperStream.close();
98              }
99          } finally {
100             wrappedStream.close();
101         }
102     }
103 
104 }