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.http.client.entity;
28
29 import java.io.IOException;
30 import java.io.InputStream;
31
32
33
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 }