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