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
28 package org.apache.hc.client5.http.impl.io;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32
33 import org.apache.hc.client5.http.impl.Wire;
34
35 class LoggingInputStream extends InputStream {
36
37 private final InputStream in;
38 private final Wire wire;
39
40 public LoggingInputStream(final InputStream in, final Wire wire) {
41 super();
42 this.in = in;
43 this.wire = wire;
44 }
45
46 @Override
47 public int read() throws IOException {
48 try {
49 final int b = in.read();
50 if (b == -1) {
51 wire.input("end of stream");
52 } else {
53 wire.input(b);
54 }
55 return b;
56 } catch (final IOException ex) {
57 wire.input("[read] I/O error: " + ex.getMessage());
58 throw ex;
59 }
60 }
61
62 @Override
63 public int read(final byte[] b) throws IOException {
64 try {
65 final int bytesRead = in.read(b);
66 if (bytesRead == -1) {
67 wire.input("end of stream");
68 } else if (bytesRead > 0) {
69 wire.input(b, 0, bytesRead);
70 }
71 return bytesRead;
72 } catch (final IOException ex) {
73 wire.input("[read] I/O error: " + ex.getMessage());
74 throw ex;
75 }
76 }
77
78 @Override
79 public int read(final byte[] b, final int off, final int len) throws IOException {
80 try {
81 final int bytesRead = in.read(b, off, len);
82 if (bytesRead == -1) {
83 wire.input("end of stream");
84 } else if (bytesRead > 0) {
85 wire.input(b, off, bytesRead);
86 }
87 return bytesRead;
88 } catch (final IOException ex) {
89 wire.input("[read] I/O error: " + ex.getMessage());
90 throw ex;
91 }
92 }
93
94 @Override
95 public long skip(final long n) throws IOException {
96 try {
97 return super.skip(n);
98 } catch (final IOException ex) {
99 wire.input("[skip] I/O error: " + ex.getMessage());
100 throw ex;
101 }
102 }
103
104 @Override
105 public int available() throws IOException {
106 try {
107 return in.available();
108 } catch (final IOException ex) {
109 wire.input("[available] I/O error : " + ex.getMessage());
110 throw ex;
111 }
112 }
113
114 @Override
115 public void mark(final int readlimit) {
116 super.mark(readlimit);
117 }
118
119 @Override
120 public void reset() throws IOException {
121 super.reset();
122 }
123
124 @Override
125 public boolean markSupported() {
126 return false;
127 }
128
129 @Override
130 public void close() throws IOException {
131 try {
132 in.close();
133 } catch (final IOException ex) {
134 wire.input("[close] I/O error: " + ex.getMessage());
135 throw ex;
136 }
137 }
138
139 }