/lib/src/org/apache/http/impl/conn/Wire.java

http://github.com/onedanshow/Screen-Courter · Java · 161 lines · 110 code · 20 blank · 31 comment · 28 complexity · a545d42730ab00b4c89a44b307436446 MD5 · raw file

  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. package org.apache.http.impl.conn;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.io.ByteArrayInputStream;
  30. import org.apache.http.annotation.Immutable;
  31. import org.apache.commons.logging.Log;
  32. /**
  33. * Logs data to the wire LOG.
  34. *
  35. *
  36. * @since 4.0
  37. */
  38. @Immutable
  39. public class Wire {
  40. private final Log log;
  41. public Wire(Log log) {
  42. this.log = log;
  43. }
  44. private void wire(String header, InputStream instream)
  45. throws IOException {
  46. StringBuilder buffer = new StringBuilder();
  47. int ch;
  48. while ((ch = instream.read()) != -1) {
  49. if (ch == 13) {
  50. buffer.append("[\\r]");
  51. } else if (ch == 10) {
  52. buffer.append("[\\n]\"");
  53. buffer.insert(0, "\"");
  54. buffer.insert(0, header);
  55. log.debug(buffer.toString());
  56. buffer.setLength(0);
  57. } else if ((ch < 32) || (ch > 127)) {
  58. buffer.append("[0x");
  59. buffer.append(Integer.toHexString(ch));
  60. buffer.append("]");
  61. } else {
  62. buffer.append((char) ch);
  63. }
  64. }
  65. if (buffer.length() > 0) {
  66. buffer.append('\"');
  67. buffer.insert(0, '\"');
  68. buffer.insert(0, header);
  69. log.debug(buffer.toString());
  70. }
  71. }
  72. public boolean enabled() {
  73. return log.isDebugEnabled();
  74. }
  75. public void output(InputStream outstream)
  76. throws IOException {
  77. if (outstream == null) {
  78. throw new IllegalArgumentException("Output may not be null");
  79. }
  80. wire(">> ", outstream);
  81. }
  82. public void input(InputStream instream)
  83. throws IOException {
  84. if (instream == null) {
  85. throw new IllegalArgumentException("Input may not be null");
  86. }
  87. wire("<< ", instream);
  88. }
  89. public void output(byte[] b, int off, int len)
  90. throws IOException {
  91. if (b == null) {
  92. throw new IllegalArgumentException("Output may not be null");
  93. }
  94. wire(">> ", new ByteArrayInputStream(b, off, len));
  95. }
  96. public void input(byte[] b, int off, int len)
  97. throws IOException {
  98. if (b == null) {
  99. throw new IllegalArgumentException("Input may not be null");
  100. }
  101. wire("<< ", new ByteArrayInputStream(b, off, len));
  102. }
  103. public void output(byte[] b)
  104. throws IOException {
  105. if (b == null) {
  106. throw new IllegalArgumentException("Output may not be null");
  107. }
  108. wire(">> ", new ByteArrayInputStream(b));
  109. }
  110. public void input(byte[] b)
  111. throws IOException {
  112. if (b == null) {
  113. throw new IllegalArgumentException("Input may not be null");
  114. }
  115. wire("<< ", new ByteArrayInputStream(b));
  116. }
  117. public void output(int b)
  118. throws IOException {
  119. output(new byte[] {(byte) b});
  120. }
  121. public void input(int b)
  122. throws IOException {
  123. input(new byte[] {(byte) b});
  124. }
  125. @Deprecated
  126. public void output(final String s)
  127. throws IOException {
  128. if (s == null) {
  129. throw new IllegalArgumentException("Output may not be null");
  130. }
  131. output(s.getBytes());
  132. }
  133. @Deprecated
  134. public void input(final String s)
  135. throws IOException {
  136. if (s == null) {
  137. throw new IllegalArgumentException("Input may not be null");
  138. }
  139. input(s.getBytes());
  140. }
  141. }