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 */ 45public class Wire { 46 47 private final Log log; 48 49 public Wire(Log log) { 50 this.log = log; 51 } 52 53 private void wire(String header, InputStream instream) 54 throws IOException { 55 StringBuilder buffer = new StringBuilder(); 56 int ch; 57 while ((ch = instream.read()) != -1) { 58 if (ch == 13) { 59 buffer.append("[\\r]"); 60 } else if (ch == 10) { 61 buffer.append("[\\n]\""); 62 buffer.insert(0, "\""); 63 buffer.insert(0, header); 64 log.debug(buffer.toString()); 65 buffer.setLength(0); 66 } else if ((ch < 32) || (ch > 127)) { 67 buffer.append("[0x"); 68 buffer.append(Integer.toHexString(ch)); 69 buffer.append("]"); 70 } else { 71 buffer.append((char) ch); 72 } 73 } 74 if (buffer.length() > 0) { 75 buffer.append('\"'); 76 buffer.insert(0, '\"'); 77 buffer.insert(0, header); 78 log.debug(buffer.toString()); 79 } 80 } 81 82 83 public boolean enabled() { 84 return log.isDebugEnabled(); 85 } 86 87 public void output(InputStream outstream) 88 throws IOException { 89 if (outstream == null) { 90 throw new IllegalArgumentException("Output may not be null"); 91 } 92 wire(">> ", outstream); 93 } 94 95 public void input(InputStream instream) 96 throws IOException { 97 if (instream == null) { 98 throw new IllegalArgumentException("Input may not be null"); 99 } 100 wire("<< ", instream); 101 } 102 103 public void output(byte[] b, int off, int len) 104 throws IOException { 105 if (b == null) { 106 throw new IllegalArgumentException("Output may not be null"); 107 } 108 wire(">> ", new ByteArrayInputStream(b, off, len)); 109 } 110 111 public void input(byte[] b, int off, int len) 112 throws IOException { 113 if (b == null) { 114 throw new IllegalArgumentException("Input may not be null"); 115 } 116 wire("<< ", new ByteArrayInputStream(b, off, len)); 117 } 118 119 public void output(byte[] b) 120 throws IOException { 121 if (b == null) { 122 throw new IllegalArgumentException("Output may not be null"); 123 } 124 wire(">> ", new ByteArrayInputStream(b)); 125 } 126 127 public void input(byte[] b) 128 throws IOException { 129 if (b == null) { 130 throw new IllegalArgumentException("Input may not be null"); 131 } 132 wire("<< ", new ByteArrayInputStream(b)); 133 } 134 135 public void output(int b) 136 throws IOException { 137 output(new byte[] {(byte) b}); 138 } 139 140 public void input(int b) 141 throws IOException { 142 input(new byte[] {(byte) b}); 143 } 144 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 public void input(final String s) 154 throws IOException { 155 if (s == null) { 156 throw new IllegalArgumentException("Input may not be null"); 157 } 158 input(s.getBytes()); 159 } 160} 161