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