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.impl.conn;
28
29 import java.io.IOException;
30
31 import org.apache.http.Consts;
32 import org.apache.http.annotation.Immutable;
33
34 import org.apache.http.io.EofSensor;
35 import org.apache.http.io.HttpTransportMetrics;
36 import org.apache.http.io.SessionInputBuffer;
37 import org.apache.http.util.CharArrayBuffer;
38
39
40
41
42
43
44
45 @Immutable
46 public class LoggingSessionInputBuffer implements SessionInputBuffer, EofSensor {
47
48
49 private final SessionInputBuffer in;
50
51 private final EofSensor eofSensor;
52
53
54 private final Wire wire;
55
56 private final String charset;
57
58
59
60
61
62
63
64 public LoggingSessionInputBuffer(
65 final SessionInputBuffer in, final Wire wire, final String charset) {
66 super();
67 this.in = in;
68 this.eofSensor = in instanceof EofSensor ? (EofSensor) in : null;
69 this.wire = wire;
70 this.charset = charset != null ? charset : Consts.ASCII.name();
71 }
72
73 public LoggingSessionInputBuffer(final SessionInputBuffer in, final Wire wire) {
74 this(in, wire, null);
75 }
76
77 public boolean isDataAvailable(int timeout) throws IOException {
78 return this.in.isDataAvailable(timeout);
79 }
80
81 public int read(byte[] b, int off, int len) throws IOException {
82 int l = this.in.read(b, off, len);
83 if (this.wire.enabled() && l > 0) {
84 this.wire.input(b, off, l);
85 }
86 return l;
87 }
88
89 public int read() throws IOException {
90 int l = this.in.read();
91 if (this.wire.enabled() && l != -1) {
92 this.wire.input(l);
93 }
94 return l;
95 }
96
97 public int read(byte[] b) throws IOException {
98 int l = this.in.read(b);
99 if (this.wire.enabled() && l > 0) {
100 this.wire.input(b, 0, l);
101 }
102 return l;
103 }
104
105 public String readLine() throws IOException {
106 String s = this.in.readLine();
107 if (this.wire.enabled() && s != null) {
108 String tmp = s + "\r\n";
109 this.wire.input(tmp.getBytes(this.charset));
110 }
111 return s;
112 }
113
114 public int readLine(final CharArrayBuffer buffer) throws IOException {
115 int l = this.in.readLine(buffer);
116 if (this.wire.enabled() && l >= 0) {
117 int pos = buffer.length() - l;
118 String s = new String(buffer.buffer(), pos, l);
119 String tmp = s + "\r\n";
120 this.wire.input(tmp.getBytes(this.charset));
121 }
122 return l;
123 }
124
125 public HttpTransportMetrics getMetrics() {
126 return this.in.getMetrics();
127 }
128
129 public boolean isEof() {
130 if (this.eofSensor != null) {
131 return this.eofSensor.isEof();
132 } else {
133 return false;
134 }
135 }
136
137 }