InputStream.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* InputStream.java -- Base class for input
  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 read
  34. * input as a stream of bytes. It provides a common set of methods for
  35. * reading bytes from streams. Subclasses implement and extend these
  36. * methods to read bytes from a particular input source such as a file
  37. * or network connection.
  38. *
  39. * @author Aaron M. Renn (arenn@urbanophile.com)
  40. * @author Warren Levy (warrenl@cygnus.com)
  41. */
  42. public abstract class InputStream implements Closeable
  43. {
  44. /**
  45. * Default, no-arg, public constructor
  46. */
  47. public InputStream()
  48. {
  49. }
  50. /**
  51. * This method returns the number of bytes that can be read from this
  52. * stream before a read can block. A return of 0 indicates that blocking
  53. * might (or might not) occur on the very next read attempt.
  54. * <p>
  55. * This method always returns 0 in this class
  56. *
  57. * @return The number of bytes that can be read before blocking could occur
  58. *
  59. * @exception IOException If an error occurs
  60. */
  61. public int available() throws IOException
  62. {
  63. return 0;
  64. }
  65. /**
  66. * This method closes the stream. Any futher attempts to read from the
  67. * stream may generate an <code>IOException</code>
  68. * <p>
  69. * This method does nothing in this class, but subclasses may override
  70. * this method in order to provide additional functionality.
  71. *
  72. * @exception IOException If an error occurs, which can only happen
  73. * in a subclass
  74. */
  75. public void close() throws IOException
  76. {
  77. // Do nothing
  78. }
  79. /**
  80. * This method marks a position in the input to which the stream can
  81. * be "reset" by calling the <code>reset()</code> method. The
  82. * parameter @code{readlimit} is the number of bytes that can be read
  83. * from the stream after setting the mark before the mark becomes
  84. * invalid. For example, if <code>mark()</code> is called with a
  85. * read limit of 10, then when 11 bytes of data are read from the
  86. * stream before the <code>reset()</code> method is called, then the
  87. * mark is invalid and the stream object instance is not required to
  88. * remember the mark.
  89. * <p>
  90. * This method does nothing in this class, but subclasses may override it
  91. * to provide mark/reset functionality.
  92. *
  93. * @param readLimit The number of bytes that can be read before the
  94. * mark becomes invalid
  95. */
  96. public void mark(int readLimit)
  97. {
  98. // Do nothing
  99. }
  100. /**
  101. * This method returns a boolean that indicates whether the mark/reset
  102. * methods are supported in this class. Those methods can be used to
  103. * remember a specific point in the stream and reset the stream to that
  104. * point.
  105. * <p>
  106. * This method always returns <code>false</code> in this class, but
  107. * subclasses can override this method to return <code>true</code>
  108. * if they support mark/reset functionality.
  109. *
  110. * @return <code>true</code> if mark/reset functionality is
  111. * supported, <code>false</code> otherwise
  112. */
  113. public boolean markSupported()
  114. {
  115. return false;
  116. }
  117. /**
  118. * This method reads an unsigned byte from the input stream and returns it
  119. * as an int in the range of 0-255. This method also will return -1 if
  120. * the end of the stream has been reached.
  121. * <p>
  122. * This method will block until the byte can be read.
  123. *
  124. * @return The byte read or -1 if end of stream
  125. *
  126. * @exception IOException If an error occurs
  127. */
  128. public abstract int read() throws IOException;
  129. /**
  130. * This method reads bytes from a stream and stores them into a caller
  131. * supplied buffer. This method attempts to completely fill the buffer,
  132. * but can return before doing so. The actual number of bytes read is
  133. * returned as an int. A -1 is returned to indicate the end of the stream.
  134. * <p>
  135. * This method will block until some data can be read.
  136. * <p>
  137. * This method operates by calling an overloaded read method like so:
  138. * <code>read(b, 0, b.length)</code>
  139. *
  140. * @param b The buffer into which the bytes read will be stored.
  141. *
  142. * @return The number of bytes read or -1 if end of stream.
  143. *
  144. * @exception IOException If an error occurs.
  145. */
  146. public int read(byte[] b) throws IOException
  147. {
  148. return read(b, 0, b.length);
  149. }
  150. /**
  151. * This method read bytes from a stream and stores them into a
  152. * caller supplied buffer. It starts storing the data at index
  153. * <code>off</code> into the buffer and attempts to read
  154. * <code>len</code> bytes. This method can return before reading the
  155. * number of bytes requested. The actual number of bytes read is
  156. * returned as an int. A -1 is returned to indicate the end of the
  157. * stream.
  158. * <p>
  159. * This method will block until some data can be read.
  160. * <p>
  161. * This method operates by calling the single byte <code>read()</code> method
  162. * in a loop until the desired number of bytes are read. The read loop
  163. * stops short if the end of the stream is encountered or if an IOException
  164. * is encountered on any read operation except the first. If the first
  165. * attempt to read a bytes fails, the IOException is allowed to propagate
  166. * upward. And subsequent IOException is caught and treated identically
  167. * to an end of stream condition. Subclasses can (and should if possible)
  168. * override this method to provide a more efficient implementation.
  169. *
  170. * @param b The array into which the bytes read should be stored
  171. * @param off The offset into the array to start storing bytes
  172. * @param len The requested number of bytes to read
  173. *
  174. * @return The actual number of bytes read, or -1 if end of stream.
  175. *
  176. * @exception IOException If an error occurs.
  177. */
  178. public int read(byte[] b, int off, int len) throws IOException
  179. {
  180. if (off < 0 || len < 0 || b.length - off < len)
  181. throw new IndexOutOfBoundsException();
  182. int i, ch;
  183. for (i = 0; i < len; ++i)
  184. try
  185. {
  186. if ((ch = read()) < 0)
  187. return i == 0 ? -1 : i; // EOF
  188. b[off + i] = (byte) ch;
  189. }
  190. catch (IOException ex)
  191. {
  192. // Only reading the first byte should cause an IOException.
  193. if (i == 0)
  194. throw ex;
  195. return i;
  196. }
  197. return i;
  198. }
  199. /**
  200. * This method resets a stream to the point where the
  201. * <code>mark()</code> method was called. Any bytes that were read
  202. * after the mark point was set will be re-read during subsequent
  203. * reads.
  204. * <p>
  205. * This method always throws an IOException in this class, but subclasses
  206. * can override this method if they provide mark/reset functionality.
  207. *
  208. * @exception IOException Always thrown for this class
  209. */
  210. public void reset() throws IOException
  211. {
  212. throw new IOException("mark/reset not supported");
  213. }
  214. /**
  215. * This method skips the specified number of bytes in the stream. It
  216. * returns the actual number of bytes skipped, which may be less than the
  217. * requested amount.
  218. * <p>
  219. * This method reads and discards bytes into a byte array until the
  220. * specified number of bytes were skipped or until either the end of stream
  221. * is reached or a read attempt returns a short count. Subclasses can
  222. * override this metho to provide a more efficient implementation where
  223. * one exists.
  224. *
  225. * @param n The requested number of bytes to skip
  226. *
  227. * @return The actual number of bytes skipped.
  228. *
  229. * @exception IOException If an error occurs
  230. */
  231. public long skip(long n) throws IOException
  232. {
  233. // Throw away n bytes by reading them into a temp byte[].
  234. // Limit the temp array to 2Kb so we don't grab too much memory.
  235. final int buflen = n > 2048 ? 2048 : (int) n;
  236. byte[] tmpbuf = new byte[buflen];
  237. final long origN = n;
  238. while (n > 0L)
  239. {
  240. int numread = read(tmpbuf, 0, n > buflen ? buflen : (int) n);
  241. if (numread <= 0)
  242. break;
  243. n -= numread;
  244. }
  245. return origN - n;
  246. }
  247. }