1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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 }