View Javadoc

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