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  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   * Logs data to the wire LOG.
39   * TODO: make package private. Should not be part of the public API.
40   *
41   * @since 4.0
42   */
43  @Immutable
44  public class Wire {
45  
46      private final Log log;
47      private final String id;
48  
49      /**
50       * @since 4.3
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     public void output(final String s)
142       throws IOException {
143         Args.notNull(s, "Output");
144         output(s.getBytes());
145     }
146 
147     public void input(final String s)
148       throws IOException {
149         Args.notNull(s, "Input");
150         input(s.getBytes());
151     }
152 }