View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Wire.java,v 1.8 2004/04/18 23:51:35 jsdever Exp $
3    * $Revision: 1.8 $
4    * $Date: 2004/04/18 23:51:35 $
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   */
29  
30  package org.apache.commons.httpclient;
31  
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.io.ByteArrayInputStream;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  /***
39   * Logs data to the wire LOG.
40   *
41   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
42   * 
43   * @since 2.0beta1
44   */
45  class Wire {
46  
47      /*** Log for any wire messages. */
48      private static final Log WIRE_LOG = LogFactory.getLog("httpclient.wire");
49  
50      private static void wire(String header, InputStream instream)
51        throws IOException {
52          StringBuffer buffer = new StringBuffer();
53          int ch;
54          while ((ch = instream.read()) != -1) {
55              if (ch == 13) {
56                  buffer.append("[//r]");
57              } else if (ch == 10) {
58                      buffer.append("[//n]\"");
59                      buffer.insert(0, "\"");
60                      buffer.insert(0, header);
61                      WIRE_LOG.debug(buffer.toString());
62                      buffer.setLength(0);
63              } else if ((ch < 32) || (ch > 127)) {
64                  buffer.append("[0x");
65                  buffer.append(Integer.toHexString(ch));
66                  buffer.append("]");
67              } else {
68                  buffer.append((char) ch);
69              }
70          } 
71          if (buffer.length() > 0) {
72              buffer.append("\"");
73              buffer.insert(0, "\"");
74              buffer.insert(0, header);
75              WIRE_LOG.debug(buffer.toString());
76          }
77      }
78  
79  
80      public static final boolean enabled() {
81          return WIRE_LOG.isDebugEnabled();
82      }    
83      
84      public static final boolean traceEnabled() {
85          return WIRE_LOG.isTraceEnabled();
86      }    
87      
88      public static final void output(InputStream outstream)
89        throws IOException {
90          if (outstream == null) {
91              throw new IllegalArgumentException("Output may not be null"); 
92          }
93          wire(">> ", outstream);
94      }
95  
96      public static final void input(InputStream instream)
97        throws IOException {
98          if (instream == null) {
99              throw new IllegalArgumentException("Input may not be null"); 
100         }
101         wire("<< ", instream);
102     }
103 
104     public static final void output(byte[] b, int off, int len)
105       throws IOException {
106         if (b == null) {
107             throw new IllegalArgumentException("Output may not be null"); 
108         }
109         wire(">> ", new ByteArrayInputStream(b, off, len));
110     }
111 
112     public static final void input(byte[] b, int off, int len)
113       throws IOException {
114         if (b == null) {
115             throw new IllegalArgumentException("Input may not be null"); 
116         }
117         wire("<< ", new ByteArrayInputStream(b, off, len));
118     }
119 
120     public static final void output(byte[] b)
121       throws IOException {
122         if (b == null) {
123             throw new IllegalArgumentException("Output may not be null"); 
124         }
125         wire(">> ", new ByteArrayInputStream(b));
126     }
127 
128     public static final void input(byte[] b)
129       throws IOException {
130         if (b == null) {
131             throw new IllegalArgumentException("Input may not be null"); 
132         }
133         wire("<< ", new ByteArrayInputStream(b));
134     }
135 
136     public static final void output(int b)
137       throws IOException {
138         output(new byte[] {(byte) b});
139     }
140 
141     public static final void input(int b)
142       throws IOException {
143         input(new byte[] {(byte) b});
144     }
145 
146     public static final void output(final String s)
147       throws IOException {
148         if (s == null) {
149             throw new IllegalArgumentException("Output may not be null"); 
150         }
151         output(s.getBytes());
152     }
153 
154     public static final void input(final String s)
155       throws IOException {
156         if (s == null) {
157             throw new IllegalArgumentException("Input may not be null"); 
158         }
159         input(s.getBytes());
160     }
161 }