1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Wire.java,v 1.4.2.1 2003/08/27 14:07:00 oglueck Exp $
3    * $Revision: 1.4.2.1 $
4    * $Date: 2003/08/27 14:07:00 $
5    *
6    * ====================================================================
7    *
8    * The Apache Software License, Version 1.1
9    *
10   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
11   * reserved.
12   *
13   * Redistribution and use in source and binary forms, with or without
14   * modification, are permitted provided that the following conditions
15   * are met:
16   *
17   * 1. Redistributions of source code must retain the above copyright
18   *    notice, this list of conditions and the following disclaimer.
19   *
20   * 2. Redistributions in binary form must reproduce the above copyright
21   *    notice, this list of conditions and the following disclaimer in
22   *    the documentation and/or other materials provided with the
23   *    distribution.
24   *
25   * 3. The end-user documentation included with the redistribution, if
26   *    any, must include the following acknowlegement:
27   *       "This product includes software developed by the
28   *        Apache Software Foundation (http://www.apache.org/)."
29   *    Alternately, this acknowlegement may appear in the software itself,
30   *    if and wherever such third-party acknowlegements normally appear.
31   *
32   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33   *    Foundation" must not be used to endorse or promote products derived
34   *    from this software without prior written permission. For written
35   *    permission, please contact apache@apache.org.
36   *
37   * 5. Products derived from this software may not be called "Apache"
38   *    nor may "Apache" appear in their names without prior written
39   *    permission of the Apache Group.
40   *
41   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52   * SUCH DAMAGE.
53   * ====================================================================
54   *
55   * This software consists of voluntary contributions made by many
56   * individuals on behalf of the Apache Software Foundation.  For more
57   * information on the Apache Software Foundation, please see
58   * <http://www.apache.org/>.
59   *
60   * [Additional notices, if required by prior licensing conditions]
61   *
62   */
63  
64  package org.apache.commons.httpclient;
65  
66  import java.io.IOException;
67  import java.io.InputStream;
68  import java.io.ByteArrayInputStream;
69  import java.io.Reader;
70  import java.io.InputStreamReader;
71  import java.io.UnsupportedEncodingException;
72  import org.apache.commons.logging.Log;
73  import org.apache.commons.logging.LogFactory;
74  
75  /***
76   * Logs data to the wire LOG.
77   *
78   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
79   * 
80   * @since 2.0beta1
81   */
82  
83  class Wire {
84  
85      /*** Log for any wire messages. */
86      private static final Log WIRE_LOG = LogFactory.getLog("httpclient.wire");
87  
88      private static void wire(String header, InputStream instream)
89        throws IOException {
90          Reader reader = null;
91          try {
92              reader = new InputStreamReader(instream, "US-ASCII");
93          } catch (UnsupportedEncodingException e) {
94              reader = new InputStreamReader(instream);
95          }
96          StringBuffer buffer = new StringBuffer();
97          int ch;
98          while ((ch = reader.read()) != -1) {
99              if (ch == 13) {
100                 buffer.append("[//r]");
101             } else if (ch == 10) {
102                     buffer.append("[//n]\"");
103                     buffer.insert(0, "\"");
104                     buffer.insert(0, header);
105                     WIRE_LOG.debug(buffer.toString());
106                     buffer.setLength(0);
107             } else if ((ch < 32) || (ch > 127)) {
108                 buffer.append("[0x");
109                 buffer.append(Integer.toHexString(ch));
110                 buffer.append("]");
111             } else {
112                 buffer.append((char) ch);
113             }
114         } 
115         if (buffer.length() > 0) {
116             buffer.append("\"");
117             buffer.insert(0, "\"");
118             buffer.insert(0, header);
119             WIRE_LOG.debug(buffer.toString());
120         }
121     }
122 
123 
124     public static final boolean enabled() {
125         return WIRE_LOG.isDebugEnabled();
126     }    
127     
128     public static final void output(InputStream outstream)
129       throws IOException {
130         if (outstream == null) {
131             throw new IllegalArgumentException("Output may not be null"); 
132         }
133         wire(">> ", outstream);
134     }
135 
136     public static final void input(InputStream instream)
137       throws IOException {
138         if (instream == null) {
139             throw new IllegalArgumentException("Input may not be null"); 
140         }
141         wire("<< ", instream);
142     }
143 
144     public static final void output(byte[] b, int off, int len)
145       throws IOException {
146         if (b == null) {
147             throw new IllegalArgumentException("Output may not be null"); 
148         }
149         wire(">> ", new ByteArrayInputStream(b, off, len));
150     }
151 
152     public static final void input(byte[] b, int off, int len)
153       throws IOException {
154         if (b == null) {
155             throw new IllegalArgumentException("Input may not be null"); 
156         }
157         wire("<< ", new ByteArrayInputStream(b, off, len));
158     }
159 
160     public static final void output(byte[] b)
161       throws IOException {
162         if (b == null) {
163             throw new IllegalArgumentException("Output may not be null"); 
164         }
165         wire(">> ", new ByteArrayInputStream(b));
166     }
167 
168     public static final void input(byte[] b)
169       throws IOException {
170         if (b == null) {
171             throw new IllegalArgumentException("Input may not be null"); 
172         }
173         wire("<< ", new ByteArrayInputStream(b));
174     }
175 
176     public static final void output(int b)
177       throws IOException {
178         output(new byte[] {(byte) b});
179     }
180 
181     public static final void input(int b)
182       throws IOException {
183         input(new byte[] {(byte) b});
184     }
185 
186     public static final void output(final String s)
187       throws IOException {
188         if (s == null) {
189             throw new IllegalArgumentException("Output may not be null"); 
190         }
191         output(s.getBytes());
192     }
193 
194     public static final void input(final String s)
195       throws IOException {
196         if (s == null) {
197             throw new IllegalArgumentException("Input may not be null"); 
198         }
199         input(s.getBytes());
200     }
201 }
This page was automatically generated by Maven