PageRenderTime 91ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/java-1.7.0-openjdk/openjdk/jdk/src/share/classes/java/io/FilterOutputStream.java

#
Java | 162 lines | 31 code | 9 blank | 122 comment | 2 complexity | 4f3d0588a88c9279157c2d634da53d67 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0
  1. /*
  2. * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package java.io;
  26. /**
  27. * This class is the superclass of all classes that filter output
  28. * streams. These streams sit on top of an already existing output
  29. * stream (the <i>underlying</i> output stream) which it uses as its
  30. * basic sink of data, but possibly transforming the data along the
  31. * way or providing additional functionality.
  32. * <p>
  33. * The class <code>FilterOutputStream</code> itself simply overrides
  34. * all methods of <code>OutputStream</code> with versions that pass
  35. * all requests to the underlying output stream. Subclasses of
  36. * <code>FilterOutputStream</code> may further override some of these
  37. * methods as well as provide additional methods and fields.
  38. *
  39. * @author Jonathan Payne
  40. * @since JDK1.0
  41. */
  42. public
  43. class FilterOutputStream extends OutputStream {
  44. /**
  45. * The underlying output stream to be filtered.
  46. */
  47. protected OutputStream out;
  48. /**
  49. * Creates an output stream filter built on top of the specified
  50. * underlying output stream.
  51. *
  52. * @param out the underlying output stream to be assigned to
  53. * the field <tt>this.out</tt> for later use, or
  54. * <code>null</code> if this instance is to be
  55. * created without an underlying stream.
  56. */
  57. public FilterOutputStream(OutputStream out) {
  58. this.out = out;
  59. }
  60. /**
  61. * Writes the specified <code>byte</code> to this output stream.
  62. * <p>
  63. * The <code>write</code> method of <code>FilterOutputStream</code>
  64. * calls the <code>write</code> method of its underlying output stream,
  65. * that is, it performs <tt>out.write(b)</tt>.
  66. * <p>
  67. * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
  68. *
  69. * @param b the <code>byte</code>.
  70. * @exception IOException if an I/O error occurs.
  71. */
  72. public void write(int b) throws IOException {
  73. out.write(b);
  74. }
  75. /**
  76. * Writes <code>b.length</code> bytes to this output stream.
  77. * <p>
  78. * The <code>write</code> method of <code>FilterOutputStream</code>
  79. * calls its <code>write</code> method of three arguments with the
  80. * arguments <code>b</code>, <code>0</code>, and
  81. * <code>b.length</code>.
  82. * <p>
  83. * Note that this method does not call the one-argument
  84. * <code>write</code> method of its underlying stream with the single
  85. * argument <code>b</code>.
  86. *
  87. * @param b the data to be written.
  88. * @exception IOException if an I/O error occurs.
  89. * @see java.io.FilterOutputStream#write(byte[], int, int)
  90. */
  91. public void write(byte b[]) throws IOException {
  92. write(b, 0, b.length);
  93. }
  94. /**
  95. * Writes <code>len</code> bytes from the specified
  96. * <code>byte</code> array starting at offset <code>off</code> to
  97. * this output stream.
  98. * <p>
  99. * The <code>write</code> method of <code>FilterOutputStream</code>
  100. * calls the <code>write</code> method of one argument on each
  101. * <code>byte</code> to output.
  102. * <p>
  103. * Note that this method does not call the <code>write</code> method
  104. * of its underlying input stream with the same arguments. Subclasses
  105. * of <code>FilterOutputStream</code> should provide a more efficient
  106. * implementation of this method.
  107. *
  108. * @param b the data.
  109. * @param off the start offset in the data.
  110. * @param len the number of bytes to write.
  111. * @exception IOException if an I/O error occurs.
  112. * @see java.io.FilterOutputStream#write(int)
  113. */
  114. public void write(byte b[], int off, int len) throws IOException {
  115. if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
  116. throw new IndexOutOfBoundsException();
  117. for (int i = 0 ; i < len ; i++) {
  118. write(b[off + i]);
  119. }
  120. }
  121. /**
  122. * Flushes this output stream and forces any buffered output bytes
  123. * to be written out to the stream.
  124. * <p>
  125. * The <code>flush</code> method of <code>FilterOutputStream</code>
  126. * calls the <code>flush</code> method of its underlying output stream.
  127. *
  128. * @exception IOException if an I/O error occurs.
  129. * @see java.io.FilterOutputStream#out
  130. */
  131. public void flush() throws IOException {
  132. out.flush();
  133. }
  134. /**
  135. * Closes this output stream and releases any system resources
  136. * associated with the stream.
  137. * <p>
  138. * The <code>close</code> method of <code>FilterOutputStream</code>
  139. * calls its <code>flush</code> method, and then calls the
  140. * <code>close</code> method of its underlying output stream.
  141. *
  142. * @exception IOException if an I/O error occurs.
  143. * @see java.io.FilterOutputStream#flush()
  144. * @see java.io.FilterOutputStream#out
  145. */
  146. public void close() throws IOException {
  147. try {
  148. flush();
  149. } catch (IOException ignored) {
  150. }
  151. out.close();
  152. }
  153. }