View Javadoc
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  
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 }