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