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