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 import java.io.InputStream;
31 import java.io.ByteArrayInputStream;
32
33 import org.apache.http.annotation.Immutable;
34
35 import org.apache.commons.logging.Log;
36
37
38
39
40
41
42
43 @Immutable
44 public class Wire {
45
46 private final Log log;
47
48 public Wire(Log log) {
49 this.log = log;
50 }
51
52 private void wire(String header, InputStream instream)
53 throws IOException {
54 StringBuilder buffer = new StringBuilder();
55 int ch;
56 while ((ch = instream.read()) != -1) {
57 if (ch == 13) {
58 buffer.append("[\\r]");
59 } else if (ch == 10) {
60 buffer.append("[\\n]\"");
61 buffer.insert(0, "\"");
62 buffer.insert(0, header);
63 log.debug(buffer.toString());
64 buffer.setLength(0);
65 } else if ((ch < 32) || (ch > 127)) {
66 buffer.append("[0x");
67 buffer.append(Integer.toHexString(ch));
68 buffer.append("]");
69 } else {
70 buffer.append((char) ch);
71 }
72 }
73 if (buffer.length() > 0) {
74 buffer.append('\"');
75 buffer.insert(0, '\"');
76 buffer.insert(0, header);
77 log.debug(buffer.toString());
78 }
79 }
80
81
82 public boolean enabled() {
83 return log.isDebugEnabled();
84 }
85
86 public void output(InputStream outstream)
87 throws IOException {
88 if (outstream == null) {
89 throw new IllegalArgumentException("Output may not be null");
90 }
91 wire(">> ", outstream);
92 }
93
94 public void input(InputStream instream)
95 throws IOException {
96 if (instream == null) {
97 throw new IllegalArgumentException("Input may not be null");
98 }
99 wire("<< ", instream);
100 }
101
102 public void output(byte[] b, int off, int len)
103 throws IOException {
104 if (b == null) {
105 throw new IllegalArgumentException("Output may not be null");
106 }
107 wire(">> ", new ByteArrayInputStream(b, off, len));
108 }
109
110 public void input(byte[] b, int off, int len)
111 throws IOException {
112 if (b == null) {
113 throw new IllegalArgumentException("Input may not be null");
114 }
115 wire("<< ", new ByteArrayInputStream(b, off, len));
116 }
117
118 public void output(byte[] b)
119 throws IOException {
120 if (b == null) {
121 throw new IllegalArgumentException("Output may not be null");
122 }
123 wire(">> ", new ByteArrayInputStream(b));
124 }
125
126 public void input(byte[] b)
127 throws IOException {
128 if (b == null) {
129 throw new IllegalArgumentException("Input may not be null");
130 }
131 wire("<< ", new ByteArrayInputStream(b));
132 }
133
134 public void output(int b)
135 throws IOException {
136 output(new byte[] {(byte) b});
137 }
138
139 public void input(int b)
140 throws IOException {
141 input(new byte[] {(byte) b});
142 }
143
144
145
146
147 @Deprecated
148 public void output(final String s)
149 throws IOException {
150 if (s == null) {
151 throw new IllegalArgumentException("Output may not be null");
152 }
153 output(s.getBytes());
154 }
155
156
157
158
159 @Deprecated
160 public void input(final String s)
161 throws IOException {
162 if (s == null) {
163 throw new IllegalArgumentException("Input may not be null");
164 }
165 input(s.getBytes());
166 }
167 }