View Javadoc

1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/Wire.java $
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  the License.  You may obtain a copy of the License at
14   *
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   *
17   *  Unless required by applicable law or agreed to in writing, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  
33  import java.io.IOException;
34  import java.io.InputStream;
35  import java.io.ByteArrayInputStream;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  /***
40   * Logs data to the wire LOG.
41   *
42   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
43   * 
44   * @since 2.0beta1
45   */
46  class Wire {
47  
48      public static Wire HEADER_WIRE = new Wire(LogFactory.getLog("httpclient.wire.header"));
49      
50      public static Wire CONTENT_WIRE = new Wire(LogFactory.getLog("httpclient.wire.content"));
51      
52      /*** Log for any wire messages. */
53      private Log log;
54      
55      private Wire(Log log) {
56          this.log = log;
57      }
58      
59      private void wire(String header, InputStream instream)
60        throws IOException {
61          StringBuffer buffer = new StringBuffer();
62          int ch;
63          while ((ch = instream.read()) != -1) {
64              if (ch == 13) {
65                  buffer.append("[//r]");
66              } else if (ch == 10) {
67                      buffer.append("[//n]\"");
68                      buffer.insert(0, "\"");
69                      buffer.insert(0, header);
70                      log.debug(buffer.toString());
71                      buffer.setLength(0);
72              } else if ((ch < 32) || (ch > 127)) {
73                  buffer.append("[0x");
74                  buffer.append(Integer.toHexString(ch));
75                  buffer.append("]");
76              } else {
77                  buffer.append((char) ch);
78              }
79          } 
80          if (buffer.length() > 0) {
81              buffer.append("\"");
82              buffer.insert(0, "\"");
83              buffer.insert(0, header);
84              log.debug(buffer.toString());
85          }
86      }
87  
88  
89      public boolean enabled() {
90          return log.isDebugEnabled();
91      }    
92      
93      public void output(InputStream outstream)
94        throws IOException {
95          if (outstream == null) {
96              throw new IllegalArgumentException("Output may not be null"); 
97          }
98          wire(">> ", outstream);
99      }
100 
101     public void input(InputStream instream)
102       throws IOException {
103         if (instream == null) {
104             throw new IllegalArgumentException("Input may not be null"); 
105         }
106         wire("<< ", instream);
107     }
108 
109     public void output(byte[] b, int off, int len)
110       throws IOException {
111         if (b == null) {
112             throw new IllegalArgumentException("Output may not be null"); 
113         }
114         wire(">> ", new ByteArrayInputStream(b, off, len));
115     }
116 
117     public void input(byte[] b, int off, int len)
118       throws IOException {
119         if (b == null) {
120             throw new IllegalArgumentException("Input may not be null"); 
121         }
122         wire("<< ", new ByteArrayInputStream(b, off, len));
123     }
124 
125     public void output(byte[] b)
126       throws IOException {
127         if (b == null) {
128             throw new IllegalArgumentException("Output may not be null"); 
129         }
130         wire(">> ", new ByteArrayInputStream(b));
131     }
132 
133     public void input(byte[] b)
134       throws IOException {
135         if (b == null) {
136             throw new IllegalArgumentException("Input may not be null"); 
137         }
138         wire("<< ", new ByteArrayInputStream(b));
139     }
140 
141     public void output(int b)
142       throws IOException {
143         output(new byte[] {(byte) b});
144     }
145 
146     public void input(int b)
147       throws IOException {
148         input(new byte[] {(byte) b});
149     }
150 
151     public void output(final String s)
152       throws IOException {
153         if (s == null) {
154             throw new IllegalArgumentException("Output may not be null"); 
155         }
156         output(s.getBytes());
157     }
158 
159     public void input(final String s)
160       throws IOException {
161         if (s == null) {
162             throw new IllegalArgumentException("Input may not be null"); 
163         }
164         input(s.getBytes());
165     }
166 }