1/*
2 * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/Wire.java,v 1.9 2004/06/24 21:39:52 mbecke Exp $
3 * $Revision: 653041 $
4 * $Date: 2008-05-03 03:39:28 -0700 (Sat, 03 May 2008) $
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
31package org.apache.http.impl.conn;
32
33import java.io.IOException;
34import java.io.InputStream;
35import java.io.ByteArrayInputStream;
36import org.apache.commons.logging.Log;
37
38/**
39 * Logs data to the wire LOG.
40 *
41 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
42 *
43 * @since 4.0
44 *
45 * @deprecated Please use {@link java.net.URL#openConnection} instead.
46 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
47 *     for further details.
48 */
49@Deprecated
50public class Wire {
51
52    private final Log log;
53
54    public Wire(Log log) {
55        this.log = log;
56    }
57
58    private void wire(String header, InputStream instream)
59      throws IOException {
60        StringBuilder buffer = new StringBuilder();
61        int ch;
62        while ((ch = instream.read()) != -1) {
63            if (ch == 13) {
64                buffer.append("[\\r]");
65            } else if (ch == 10) {
66                    buffer.append("[\\n]\"");
67                    buffer.insert(0, "\"");
68                    buffer.insert(0, header);
69                    log.debug(buffer.toString());
70                    buffer.setLength(0);
71            } else if ((ch < 32) || (ch > 127)) {
72                buffer.append("[0x");
73                buffer.append(Integer.toHexString(ch));
74                buffer.append("]");
75            } else {
76                buffer.append((char) ch);
77            }
78        }
79        if (buffer.length() > 0) {
80            buffer.append('\"');
81            buffer.insert(0, '\"');
82            buffer.insert(0, header);
83            log.debug(buffer.toString());
84        }
85    }
86
87
88    public boolean enabled() {
89        return log.isDebugEnabled();
90    }
91
92    public void output(InputStream outstream)
93      throws IOException {
94        if (outstream == null) {
95            throw new IllegalArgumentException("Output may not be null");
96        }
97        wire(">> ", outstream);
98    }
99
100    public void input(InputStream instream)
101      throws IOException {
102        if (instream == null) {
103            throw new IllegalArgumentException("Input may not be null");
104        }
105        wire("<< ", instream);
106    }
107
108    public void output(byte[] b, int off, int len)
109      throws IOException {
110        if (b == null) {
111            throw new IllegalArgumentException("Output may not be null");
112        }
113        wire(">> ", new ByteArrayInputStream(b, off, len));
114    }
115
116    public void input(byte[] b, int off, int len)
117      throws IOException {
118        if (b == null) {
119            throw new IllegalArgumentException("Input may not be null");
120        }
121        wire("<< ", new ByteArrayInputStream(b, off, len));
122    }
123
124    public void output(byte[] b)
125      throws IOException {
126        if (b == null) {
127            throw new IllegalArgumentException("Output may not be null");
128        }
129        wire(">> ", new ByteArrayInputStream(b));
130    }
131
132    public void input(byte[] b)
133      throws IOException {
134        if (b == null) {
135            throw new IllegalArgumentException("Input may not be null");
136        }
137        wire("<< ", new ByteArrayInputStream(b));
138    }
139
140    public void output(int b)
141      throws IOException {
142        output(new byte[] {(byte) b});
143    }
144
145    public void input(int b)
146      throws IOException {
147        input(new byte[] {(byte) b});
148    }
149
150    public void output(final String s)
151      throws IOException {
152        if (s == null) {
153            throw new IllegalArgumentException("Output may not be null");
154        }
155        output(s.getBytes());
156    }
157
158    public void input(final String s)
159      throws IOException {
160        if (s == null) {
161            throw new IllegalArgumentException("Input may not be null");
162        }
163        input(s.getBytes());
164    }
165}
166