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