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