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