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