Debian Codegraph @

httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/Wire.java

raw
1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
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 }