1 31 32 package org.apache.commons.httpclient; 33 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.ByteArrayInputStream ; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 47 48 class Wire { 49 50 public static Wire HEADER_WIRE = new Wire(LogFactory.getLog("httpclient.wire.header")); 51 52 public static Wire CONTENT_WIRE = new Wire(LogFactory.getLog("httpclient.wire.content")); 53 54 private Log log; 55 56 private Wire(Log log) { 57 this.log = log; 58 } 59 60 private void wire(String header, InputStream instream) 61 throws IOException { 62 StringBuffer buffer = new StringBuffer (); 63 int ch; 64 while ((ch = instream.read()) != -1) { 65 if (ch == 13) { 66 buffer.append("[\\r]"); 67 } else if (ch == 10) { 68 buffer.append("[\\n]\""); 69 buffer.insert(0, "\""); 70 buffer.insert(0, header); 71 log.debug(buffer.toString()); 72 buffer.setLength(0); 73 } else if ((ch < 32) || (ch > 127)) { 74 buffer.append("[0x"); 75 buffer.append(Integer.toHexString(ch)); 76 buffer.append("]"); 77 } else { 78 buffer.append((char) ch); 79 } 80 } 81 if (buffer.length() > 0) { 82 buffer.append("\""); 83 buffer.insert(0, "\""); 84 buffer.insert(0, header); 85 log.debug(buffer.toString()); 86 } 87 } 88 89 90 public boolean enabled() { 91 return log.isDebugEnabled(); 92 } 93 94 public void output(InputStream outstream) 95 throws IOException { 96 if (outstream == null) { 97 throw new IllegalArgumentException ("Output may not be null"); 98 } 99 wire(">> ", outstream); 100 } 101 102 public void input(InputStream instream) 103 throws IOException { 104 if (instream == null) { 105 throw new IllegalArgumentException ("Input may not be null"); 106 } 107 wire("<< ", instream); 108 } 109 110 public void output(byte[] b, int off, int len) 111 throws IOException { 112 if (b == null) { 113 throw new IllegalArgumentException ("Output may not be null"); 114 } 115 wire(">> ", new ByteArrayInputStream (b, off, len)); 116 } 117 118 public void input(byte[] b, int off, int len) 119 throws IOException { 120 if (b == null) { 121 throw new IllegalArgumentException ("Input may not be null"); 122 } 123 wire("<< ", new ByteArrayInputStream (b, off, len)); 124 } 125 126 public void output(byte[] b) 127 throws IOException { 128 if (b == null) { 129 throw new IllegalArgumentException ("Output may not be null"); 130 } 131 wire(">> ", new ByteArrayInputStream (b)); 132 } 133 134 public void input(byte[] b) 135 throws IOException { 136 if (b == null) { 137 throw new IllegalArgumentException ("Input may not be null"); 138 } 139 wire("<< ", new ByteArrayInputStream (b)); 140 } 141 142 public void output(int b) 143 throws IOException { 144 output(new byte[] {(byte) b}); 145 } 146 147 public void input(int b) 148 throws IOException { 149 input(new byte[] {(byte) b}); 150 } 151 152 public void output(final String s) 153 throws IOException { 154 if (s == null) { 155 throw new IllegalArgumentException ("Output may not be null"); 156 } 157 output(s.getBytes()); 158 } 159 160 public void input(final String s) 161 throws IOException { 162 if (s == null) { 163 throw new IllegalArgumentException ("Input may not be null"); 164 } 165 input(s.getBytes()); 166 } 167 } 168 | Popular Tags |