1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.io.output;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. /**
  20. * Classic splitter of OutputStream. Named after the unix 'tee'
  21. * command. It allows a stream to be branched off so there
  22. * are now two streams.
  23. *
  24. * @author <a href="mailto:bayard@apache.org">Henri Yandell</a>
  25. * @version $Id: TeeOutputStream.java,v 1.6 2004/02/23 04:53:04 bayard Exp $
  26. */
  27. public class TeeOutputStream extends ProxyOutputStream {
  28. /** the second OutputStream to write to */
  29. protected OutputStream branch;
  30. /**
  31. * Constructs a TeeOutputStream.
  32. * @param out the main OutputStream
  33. * @param branch the second OutputStream
  34. */
  35. public TeeOutputStream( OutputStream out, OutputStream branch ) {
  36. super(out);
  37. this.branch = branch;
  38. }
  39. /** @see java.io.OutputStream#write(byte[]) */
  40. public synchronized void write(byte[] b) throws IOException {
  41. super.write(b);
  42. this.branch.write(b);
  43. }
  44. /** @see java.io.OutputStream#write(byte[], int, int) */
  45. public synchronized void write(byte[] b, int off, int len) throws IOException {
  46. super.write(b, off, len);
  47. this.branch.write(b, off, len);
  48. }
  49. /** @see java.io.OutputStream#write(int) */
  50. public synchronized void write(int b) throws IOException {
  51. super.write(b);
  52. this.branch.write(b);
  53. }
  54. /**
  55. * Flushes both streams.
  56. *
  57. * @see java.io.OutputStream#flush()
  58. */
  59. public void flush() throws IOException {
  60. super.flush();
  61. this.branch.flush();
  62. }
  63. /**
  64. * Closes both streams.
  65. *
  66. * @see java.io.OutputStream#close()
  67. */
  68. public void close() throws IOException {
  69. super.close();
  70. this.branch.close();
  71. }
  72. }