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