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