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