mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/Wire.java

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

mercurial