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