1. /*
  2. * @(#)OutputStream.java 1.29 04/04/21
  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 abstract class is the superclass of all classes representing
  10. * an output stream of bytes. An output stream accepts output bytes
  11. * and sends them to some sink.
  12. * <p>
  13. * Applications that need to define a subclass of
  14. * <code>OutputStream</code> must always provide at least a method
  15. * that writes one byte of output.
  16. *
  17. * @author Arthur van Hoff
  18. * @version 1.29, 04/21/04
  19. * @see java.io.BufferedOutputStream
  20. * @see java.io.ByteArrayOutputStream
  21. * @see java.io.DataOutputStream
  22. * @see java.io.FilterOutputStream
  23. * @see java.io.InputStream
  24. * @see java.io.OutputStream#write(int)
  25. * @since JDK1.0
  26. */
  27. public abstract class OutputStream implements Closeable, Flushable {
  28. /**
  29. * Writes the specified byte to this output stream. The general
  30. * contract for <code>write</code> is that one byte is written
  31. * to the output stream. The byte to be written is the eight
  32. * low-order bits of the argument <code>b</code>. The 24
  33. * high-order bits of <code>b</code> are ignored.
  34. * <p>
  35. * Subclasses of <code>OutputStream</code> must provide an
  36. * implementation for this method.
  37. *
  38. * @param b the <code>byte</code>.
  39. * @exception IOException if an I/O error occurs. In particular,
  40. * an <code>IOException</code> may be thrown if the
  41. * output stream has been closed.
  42. */
  43. public abstract void write(int b) throws IOException;
  44. /**
  45. * Writes <code>b.length</code> bytes from the specified byte array
  46. * to this output stream. The general contract for <code>write(b)</code>
  47. * is that it should have exactly the same effect as the call
  48. * <code>write(b, 0, b.length)</code>.
  49. *
  50. * @param b the data.
  51. * @exception IOException if an I/O error occurs.
  52. * @see java.io.OutputStream#write(byte[], int, int)
  53. */
  54. public void write(byte b[]) throws IOException {
  55. write(b, 0, b.length);
  56. }
  57. /**
  58. * Writes <code>len</code> bytes from the specified byte array
  59. * starting at offset <code>off</code> to this output stream.
  60. * The general contract for <code>write(b, off, len)</code> is that
  61. * some of the bytes in the array <code>b</code> are written to the
  62. * output stream in order; element <code>b[off]</code> is the first
  63. * byte written and <code>b[off+len-1]</code> is the last byte written
  64. * by this operation.
  65. * <p>
  66. * The <code>write</code> method of <code>OutputStream</code> calls
  67. * the write method of one argument on each of the bytes to be
  68. * written out. Subclasses are encouraged to override this method and
  69. * provide a more efficient implementation.
  70. * <p>
  71. * If <code>b</code> is <code>null</code>, a
  72. * <code>NullPointerException</code> is thrown.
  73. * <p>
  74. * If <code>off</code> is negative, or <code>len</code> is negative, or
  75. * <code>off+len</code> is greater than the length of the array
  76. * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
  77. *
  78. * @param b the data.
  79. * @param off the start offset in the data.
  80. * @param len the number of bytes to write.
  81. * @exception IOException if an I/O error occurs. In particular,
  82. * an <code>IOException</code> is thrown if the output
  83. * stream is closed.
  84. */
  85. public void write(byte b[], int off, int len) throws IOException {
  86. if (b == null) {
  87. throw new NullPointerException();
  88. } else if ((off < 0) || (off > b.length) || (len < 0) ||
  89. ((off + len) > b.length) || ((off + len) < 0)) {
  90. throw new IndexOutOfBoundsException();
  91. } else if (len == 0) {
  92. return;
  93. }
  94. for (int i = 0 ; i < len ; i++) {
  95. write(b[off + i]);
  96. }
  97. }
  98. /**
  99. * Flushes this output stream and forces any buffered output bytes
  100. * to be written out. The general contract of <code>flush</code> is
  101. * that calling it is an indication that, if any bytes previously
  102. * written have been buffered by the implementation of the output
  103. * stream, such bytes should immediately be written to their
  104. * intended destination.
  105. * <p>
  106. * If the intended destination of this stream is an abstraction provided by
  107. * the underlying operating system, for example a file, then flushing the
  108. * stream guarantees only that bytes previously written to the stream are
  109. * passed to the operating system for writing; it does not guarantee that
  110. * they are actually written to a physical device such as a disk drive.
  111. * <p>
  112. * The <code>flush</code> method of <code>OutputStream</code> does nothing.
  113. *
  114. * @exception IOException if an I/O error occurs.
  115. */
  116. public void flush() throws IOException {
  117. }
  118. /**
  119. * Closes this output stream and releases any system resources
  120. * associated with this stream. The general contract of <code>close</code>
  121. * is that it closes the output stream. A closed stream cannot perform
  122. * output operations and cannot be reopened.
  123. * <p>
  124. * The <code>close</code> method of <code>OutputStream</code> does nothing.
  125. *
  126. * @exception IOException if an I/O error occurs.
  127. */
  128. public void close() throws IOException {
  129. }
  130. }