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 package org.apache.http.testserver;
29
30 import org.apache.commons.logging.Log;
31
32 class Wire {
33
34 private final Log log;
35 private final String id;
36
37 public Wire(final Log log, final String id) {
38 super();
39 this.log = log;
40 this.id = id;
41 }
42
43 private void wire(final String header, final byte[] b, final int pos, final int off) {
44 final StringBuilder buffer = new StringBuilder();
45 for (int i = 0; i < off; i++) {
46 final int ch = b[pos + i];
47 if (ch == 13) {
48 buffer.append("[\\r]");
49 } else if (ch == 10) {
50 buffer.append("[\\n]\"");
51 buffer.insert(0, "\"");
52 buffer.insert(0, header);
53 this.log.debug(this.id + " " + buffer.toString());
54 buffer.setLength(0);
55 } else if ((ch < 32) || (ch > 127)) {
56 buffer.append("[0x");
57 buffer.append(Integer.toHexString(ch));
58 buffer.append("]");
59 } else {
60 buffer.append((char) ch);
61 }
62 }
63 if (buffer.length() > 0) {
64 buffer.append('\"');
65 buffer.insert(0, '\"');
66 buffer.insert(0, header);
67 this.log.debug(this.id + " " + buffer.toString());
68 }
69 }
70
71
72 public boolean isEnabled() {
73 return this.log.isDebugEnabled();
74 }
75
76 public void output(final byte[] b, final int pos, final int off) {
77 wire("<< ", b, pos, off);
78 }
79
80 public void input(final byte[] b, final int pos, final int off) {
81 wire(">> ", b, pos, off);
82 }
83
84 public void output(final byte[] b) {
85 output(b, 0, b.length);
86 }
87
88 public void input(final byte[] b) {
89 input(b, 0, b.length);
90 }
91
92 public void output(final int b) {
93 output(new byte[] {(byte) b});
94 }
95
96 public void input(final int b) {
97 input(new byte[] {(byte) b});
98 }
99
100 }