OutputStream.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* OutputStream.java -- Base class for byte output streams
  2. Copyright (C) 1998, 1999, 2001, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. /**
  33. * This abstract class forms the base of the hierarchy of classes that
  34. * write output as a stream of bytes. It provides a common set of methods
  35. * for writing bytes to stream. Subclasses implement and/or extend these
  36. * methods to write bytes in a particular manner or to a particular
  37. * destination such as a file on disk or network connection.
  38. *
  39. * @author Aaron M. Renn (arenn@urbanophile.com)
  40. * @author Tom Tromey (tromey@cygnus.com)
  41. */
  42. public abstract class OutputStream implements Closeable, Flushable
  43. {
  44. /**
  45. * This is the default no-argument constructor for this class. This method
  46. * does nothing in this class.
  47. */
  48. public OutputStream ()
  49. {
  50. }
  51. /**
  52. * This method writes a single byte to the output stream. The byte written
  53. * is the low eight bits of the <code>int</code> passed and a argument.
  54. * <p>
  55. * Subclasses must provide an implementation of this abstract method
  56. *
  57. * @param b The byte to be written to the output stream, passed as
  58. * the low eight bits of an <code>int</code>
  59. *
  60. * @exception IOException If an error occurs
  61. */
  62. public abstract void write (int b) throws IOException;
  63. /**
  64. * This method all the writes bytes from the passed array to the
  65. * output stream. This method is equivalent to <code>write(b, 0,
  66. * buf.length)</code> which is exactly how it is implemented in this
  67. * class.
  68. *
  69. * @param b The array of bytes to write
  70. *
  71. * @exception IOException If an error occurs
  72. */
  73. public void write (byte[] b) throws IOException, NullPointerException
  74. {
  75. write (b, 0, b.length);
  76. }
  77. /**
  78. * This method writes <code>len</code> bytes from the specified array
  79. * <code>b</code> starting at index <code>off</code> into the array.
  80. * <p>
  81. * This method in this class calls the single byte <code>write()</code>
  82. * method in a loop until all bytes have been written. Subclasses should
  83. * override this method if possible in order to provide a more efficent
  84. * implementation.
  85. *
  86. * @param b The array of bytes to write from
  87. * @param off The index into the array to start writing from
  88. * @param len The number of bytes to write
  89. *
  90. * @exception IOException If an error occurs
  91. */
  92. public void write (byte[] b, int off, int len)
  93. throws IOException, NullPointerException, IndexOutOfBoundsException
  94. {
  95. if (off < 0 || len < 0 || off + len > b.length)
  96. throw new ArrayIndexOutOfBoundsException ();
  97. for (int i = 0; i < len; ++i)
  98. write (b[off + i]);
  99. }
  100. /**
  101. * This method forces any data that may have been buffered to be written
  102. * to the underlying output device. Please note that the host environment
  103. * might perform its own buffering unbeknowst to Java. In that case, a
  104. * write made (for example, to a disk drive) might be cached in OS
  105. * buffers instead of actually being written to disk.
  106. * <p>
  107. * This method in this class does nothing.
  108. *
  109. * @exception IOException If an error occurs
  110. */
  111. public void flush () throws IOException
  112. {
  113. }
  114. /**
  115. * This method closes the stream. Any internal or native resources
  116. * associated with this stream are freed. Any subsequent attempt to
  117. * access the stream might throw an exception.
  118. * <p>
  119. * This method in this class does nothing.
  120. *
  121. * @exception IOException If an error occurs
  122. */
  123. public void close () throws IOException
  124. {
  125. }
  126. }