1. /*
  2. * @(#)FilterOutputStream.java 1.31 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.io;
  8. /**
  9. * This class is the superclass of all classes that filter output
  10. * streams. These streams sit on top of an already existing output
  11. * stream (the <i>underlying</i> output stream) which it uses as its
  12. * basic sink of data, but possibly transforming the data along the
  13. * way or providing additional functionality.
  14. * <p>
  15. * The class <code>FilterOutputStream</code> itself simply overrides
  16. * all methods of <code>OutputStream</code> with versions that pass
  17. * all requests to the underlying output stream. Subclasses of
  18. * <code>FilterOutputStream</code> may further override some of these
  19. * methods as well as provide additional methods and fields.
  20. *
  21. * @author Jonathan Payne
  22. * @version 1.31, 12/19/03
  23. * @since JDK1.0
  24. */
  25. public
  26. class FilterOutputStream extends OutputStream {
  27. /**
  28. * The underlying output stream to be filtered.
  29. */
  30. protected OutputStream out;
  31. /**
  32. * Creates an output stream filter built on top of the specified
  33. * underlying output stream.
  34. *
  35. * @param out the underlying output stream to be assigned to
  36. * the field <tt>this.out</tt> for later use, or
  37. * <code>null</code> if this instance is to be
  38. * created without an underlying stream.
  39. */
  40. public FilterOutputStream(OutputStream out) {
  41. this.out = out;
  42. }
  43. /**
  44. * Writes the specified <code>byte</code> to this output stream.
  45. * <p>
  46. * The <code>write</code> method of <code>FilterOutputStream</code>
  47. * calls the <code>write</code> method of its underlying output stream,
  48. * that is, it performs <tt>out.write(b)</tt>.
  49. * <p>
  50. * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
  51. *
  52. * @param b the <code>byte</code>.
  53. * @exception IOException if an I/O error occurs.
  54. */
  55. public void write(int b) throws IOException {
  56. out.write(b);
  57. }
  58. /**
  59. * Writes <code>b.length</code> bytes to this output stream.
  60. * <p>
  61. * The <code>write</code> method of <code>FilterOutputStream</code>
  62. * calls its <code>write</code> method of three arguments with the
  63. * arguments <code>b</code>, <code>0</code>, and
  64. * <code>b.length</code>.
  65. * <p>
  66. * Note that this method does not call the one-argument
  67. * <code>write</code> method of its underlying stream with the single
  68. * argument <code>b</code>.
  69. *
  70. * @param b the data to be written.
  71. * @exception IOException if an I/O error occurs.
  72. * @see java.io.FilterOutputStream#write(byte[], int, int)
  73. */
  74. public void write(byte b[]) throws IOException {
  75. write(b, 0, b.length);
  76. }
  77. /**
  78. * Writes <code>len</code> bytes from the specified
  79. * <code>byte</code> array starting at offset <code>off</code> to
  80. * this output stream.
  81. * <p>
  82. * The <code>write</code> method of <code>FilterOutputStream</code>
  83. * calls the <code>write</code> method of one argument on each
  84. * <code>byte</code> to output.
  85. * <p>
  86. * Note that this method does not call the <code>write</code> method
  87. * of its underlying input stream with the same arguments. Subclasses
  88. * of <code>FilterOutputStream</code> should provide a more efficient
  89. * implementation of this method.
  90. *
  91. * @param b the data.
  92. * @param off the start offset in the data.
  93. * @param len the number of bytes to write.
  94. * @exception IOException if an I/O error occurs.
  95. * @see java.io.FilterOutputStream#write(int)
  96. */
  97. public void write(byte b[], int off, int len) throws IOException {
  98. if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
  99. throw new IndexOutOfBoundsException();
  100. for (int i = 0 ; i < len ; i++) {
  101. write(b[off + i]);
  102. }
  103. }
  104. /**
  105. * Flushes this output stream and forces any buffered output bytes
  106. * to be written out to the stream.
  107. * <p>
  108. * The <code>flush</code> method of <code>FilterOutputStream</code>
  109. * calls the <code>flush</code> method of its underlying output stream.
  110. *
  111. * @exception IOException if an I/O error occurs.
  112. * @see java.io.FilterOutputStream#out
  113. */
  114. public void flush() throws IOException {
  115. out.flush();
  116. }
  117. /**
  118. * Closes this output stream and releases any system resources
  119. * associated with the stream.
  120. * <p>
  121. * The <code>close</code> method of <code>FilterOutputStream</code>
  122. * calls its <code>flush</code> method, and then calls the
  123. * <code>close</code> method of its underlying output stream.
  124. *
  125. * @exception IOException if an I/O error occurs.
  126. * @see java.io.FilterOutputStream#flush()
  127. * @see java.io.FilterOutputStream#out
  128. */
  129. public void close() throws IOException {
  130. try {
  131. flush();
  132. } catch (IOException ignored) {
  133. }
  134. out.close();
  135. }
  136. }