michael@0: /* michael@0: * ==================================================================== michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more michael@0: * contributor license agreements. See the NOTICE file distributed with michael@0: * this work for additional information regarding copyright ownership. michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0 michael@0: * (the "License"); you may not use this file except in compliance with michael@0: * the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.impl.conn; michael@0: michael@0: import java.io.IOException; michael@0: import java.io.InputStream; michael@0: import java.io.ByteArrayInputStream; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable; michael@0: michael@0: import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog; michael@0: michael@0: /** michael@0: * Logs data to the wire LOG. michael@0: * michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @Immutable michael@0: public class Wire { michael@0: michael@0: public HttpClientAndroidLog log; michael@0: michael@0: public Wire(HttpClientAndroidLog log) { michael@0: this.log = log; michael@0: } michael@0: michael@0: private void wire(String header, InputStream instream) michael@0: throws IOException { michael@0: StringBuilder buffer = new StringBuilder(); michael@0: int ch; michael@0: while ((ch = instream.read()) != -1) { michael@0: if (ch == 13) { michael@0: buffer.append("[\\r]"); michael@0: } else if (ch == 10) { michael@0: buffer.append("[\\n]\""); michael@0: buffer.insert(0, "\""); michael@0: buffer.insert(0, header); michael@0: log.debug(buffer.toString()); michael@0: buffer.setLength(0); michael@0: } else if ((ch < 32) || (ch > 127)) { michael@0: buffer.append("[0x"); michael@0: buffer.append(Integer.toHexString(ch)); michael@0: buffer.append("]"); michael@0: } else { michael@0: buffer.append((char) ch); michael@0: } michael@0: } michael@0: if (buffer.length() > 0) { michael@0: buffer.append('\"'); michael@0: buffer.insert(0, '\"'); michael@0: buffer.insert(0, header); michael@0: log.debug(buffer.toString()); michael@0: } michael@0: } michael@0: michael@0: michael@0: public boolean enabled() { michael@0: return log.isDebugEnabled(); michael@0: } michael@0: michael@0: public void output(InputStream outstream) michael@0: throws IOException { michael@0: if (outstream == null) { michael@0: throw new IllegalArgumentException("Output may not be null"); michael@0: } michael@0: wire(">> ", outstream); michael@0: } michael@0: michael@0: public void input(InputStream instream) michael@0: throws IOException { michael@0: if (instream == null) { michael@0: throw new IllegalArgumentException("Input may not be null"); michael@0: } michael@0: wire("<< ", instream); michael@0: } michael@0: michael@0: public void output(byte[] b, int off, int len) michael@0: throws IOException { michael@0: if (b == null) { michael@0: throw new IllegalArgumentException("Output may not be null"); michael@0: } michael@0: wire(">> ", new ByteArrayInputStream(b, off, len)); michael@0: } michael@0: michael@0: public void input(byte[] b, int off, int len) michael@0: throws IOException { michael@0: if (b == null) { michael@0: throw new IllegalArgumentException("Input may not be null"); michael@0: } michael@0: wire("<< ", new ByteArrayInputStream(b, off, len)); michael@0: } michael@0: michael@0: public void output(byte[] b) michael@0: throws IOException { michael@0: if (b == null) { michael@0: throw new IllegalArgumentException("Output may not be null"); michael@0: } michael@0: wire(">> ", new ByteArrayInputStream(b)); michael@0: } michael@0: michael@0: public void input(byte[] b) michael@0: throws IOException { michael@0: if (b == null) { michael@0: throw new IllegalArgumentException("Input may not be null"); michael@0: } michael@0: wire("<< ", new ByteArrayInputStream(b)); michael@0: } michael@0: michael@0: public void output(int b) michael@0: throws IOException { michael@0: output(new byte[] {(byte) b}); michael@0: } michael@0: michael@0: public void input(int b) michael@0: throws IOException { michael@0: input(new byte[] {(byte) b}); michael@0: } michael@0: michael@0: @Deprecated michael@0: public void output(final String s) michael@0: throws IOException { michael@0: if (s == null) { michael@0: throw new IllegalArgumentException("Output may not be null"); michael@0: } michael@0: output(s.getBytes()); michael@0: } michael@0: michael@0: @Deprecated michael@0: public void input(final String s) michael@0: throws IOException { michael@0: if (s == null) { michael@0: throw new IllegalArgumentException("Input may not be null"); michael@0: } michael@0: input(s.getBytes()); michael@0: } michael@0: }