You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StreamSniffer.java 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. /*
  14. * StreamGrabber.java created on May 16, 2002
  15. *
  16. */
  17. package org.aspectj.testing.util;
  18. import java.io.FilterOutputStream;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. /**
  22. * Listen to a stream using StringBuffer.
  23. * Clients install and remove buffer to enable/disable listening.
  24. * Does not affect data passed to underlying stream
  25. */
  26. public class StreamSniffer extends FilterOutputStream {
  27. StringBuffer buffer;
  28. /** have to use delegate, not super, because super we will double-count input */
  29. final OutputStream delegate;
  30. public StreamSniffer(OutputStream stream) {
  31. super(stream);
  32. delegate = stream;
  33. }
  34. /** set to null to stop copying */
  35. public void setBuffer(StringBuffer sb) {
  36. buffer = sb;
  37. }
  38. //---------------- FilterOutputStream
  39. public void write(int b) throws IOException {
  40. StringBuffer sb = buffer;
  41. if (null != sb) {
  42. if ((b > Character.MAX_VALUE)
  43. || (b < Character.MIN_VALUE)) {
  44. throw new Error("don't know double-byte"); // XXX
  45. } else {
  46. sb.append((char) b);
  47. }
  48. }
  49. delegate.write(b);
  50. }
  51. public void write(byte[] b) throws IOException {
  52. StringBuffer sb = buffer;
  53. if (null != sb) {
  54. String s = new String(b);
  55. sb.append(s);
  56. }
  57. delegate.write(b);
  58. }
  59. public void write(byte[] b, int offset, int length) throws IOException {
  60. StringBuffer sb = buffer;
  61. if (null != sb) {
  62. String s = new String(b, offset, length);
  63. sb.append(s);
  64. }
  65. delegate.write(b, offset, length);
  66. }
  67. }