1. /*
  2. * @(#)InputStream.java 1.40 03/01/23
  3. *
  4. * Copyright 2003 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 input stream of bytes.
  11. *
  12. * <p> Applications that need to define a subclass of <code>InputStream</code>
  13. * must always provide a method that returns the next byte of input.
  14. *
  15. * @author Arthur van Hoff
  16. * @version 1.40, 01/23/03
  17. * @see java.io.BufferedInputStream
  18. * @see java.io.ByteArrayInputStream
  19. * @see java.io.DataInputStream
  20. * @see java.io.FilterInputStream
  21. * @see java.io.InputStream#read()
  22. * @see java.io.OutputStream
  23. * @see java.io.PushbackInputStream
  24. * @since JDK1.0
  25. */
  26. public abstract class InputStream {
  27. // SKIP_BUFFER_SIZE is used to determine the size of skipBuffer
  28. private static final int SKIP_BUFFER_SIZE = 2048;
  29. // skipBuffer is initialized in skip(long), if needed.
  30. private static byte[] skipBuffer;
  31. /**
  32. * Reads the next byte of data from the input stream. The value byte is
  33. * returned as an <code>int</code> in the range <code>0</code> to
  34. * <code>255</code>. If no byte is available because the end of the stream
  35. * has been reached, the value <code>-1</code> is returned. This method
  36. * blocks until input data is available, the end of the stream is detected,
  37. * or an exception is thrown.
  38. *
  39. * <p> A subclass must provide an implementation of this method.
  40. *
  41. * @return the next byte of data, or <code>-1</code> if the end of the
  42. * stream is reached.
  43. * @exception IOException if an I/O error occurs.
  44. */
  45. public abstract int read() throws IOException;
  46. /**
  47. * Reads some number of bytes from the input stream and stores them into
  48. * the buffer array <code>b</code>. The number of bytes actually read is
  49. * returned as an integer. This method blocks until input data is
  50. * available, end of file is detected, or an exception is thrown.
  51. *
  52. * <p> If <code>b</code> is <code>null</code>, a
  53. * <code>NullPointerException</code> is thrown. If the length of
  54. * <code>b</code> is zero, then no bytes are read and <code>0</code> is
  55. * returned; otherwise, there is an attempt to read at least one byte. If
  56. * no byte is available because the stream is at end of file, the value
  57. * <code>-1</code> is returned; otherwise, at least one byte is read and
  58. * stored into <code>b</code>.
  59. *
  60. * <p> The first byte read is stored into element <code>b[0]</code>, the
  61. * next one into <code>b[1]</code>, and so on. The number of bytes read is,
  62. * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
  63. * number of bytes actually read; these bytes will be stored in elements
  64. * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
  65. * leaving elements <code>b[</code><i>k</i><code>]</code> through
  66. * <code>b[b.length-1]</code> unaffected.
  67. *
  68. * <p> If the first byte cannot be read for any reason other than end of
  69. * file, then an <code>IOException</code> is thrown. In particular, an
  70. * <code>IOException</code> is thrown if the input stream has been closed.
  71. *
  72. * <p> The <code>read(b)</code> method for class <code>InputStream</code>
  73. * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
  74. *
  75. * @param b the buffer into which the data is read.
  76. * @return the total number of bytes read into the buffer, or
  77. * <code>-1</code> is there is no more data because the end of
  78. * the stream has been reached.
  79. * @exception IOException if an I/O error occurs.
  80. * @exception NullPointerException if <code>b</code> is <code>null</code>.
  81. * @see java.io.InputStream#read(byte[], int, int)
  82. */
  83. public int read(byte b[]) throws IOException {
  84. return read(b, 0, b.length);
  85. }
  86. /**
  87. * Reads up to <code>len</code> bytes of data from the input stream into
  88. * an array of bytes. An attempt is made to read as many as
  89. * <code>len</code> bytes, but a smaller number may be read, possibly
  90. * zero. The number of bytes actually read is returned as an integer.
  91. *
  92. * <p> This method blocks until input data is available, end of file is
  93. * detected, or an exception is thrown.
  94. *
  95. * <p> If <code>b</code> is <code>null</code>, a
  96. * <code>NullPointerException</code> is thrown.
  97. *
  98. * <p> If <code>off</code> is negative, or <code>len</code> is negative, or
  99. * <code>off+len</code> is greater than the length of the array
  100. * <code>b</code>, then an <code>IndexOutOfBoundsException</code> is
  101. * thrown.
  102. *
  103. * <p> If <code>len</code> is zero, then no bytes are read and
  104. * <code>0</code> is returned; otherwise, there is an attempt to read at
  105. * least one byte. If no byte is available because the stream is at end of
  106. * file, the value <code>-1</code> is returned; otherwise, at least one
  107. * byte is read and stored into <code>b</code>.
  108. *
  109. * <p> The first byte read is stored into element <code>b[off]</code>, the
  110. * next one into <code>b[off+1]</code>, and so on. The number of bytes read
  111. * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
  112. * bytes actually read; these bytes will be stored in elements
  113. * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
  114. * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
  115. * <code>b[off+len-1]</code> unaffected.
  116. *
  117. * <p> In every case, elements <code>b[0]</code> through
  118. * <code>b[off]</code> and elements <code>b[off+len]</code> through
  119. * <code>b[b.length-1]</code> are unaffected.
  120. *
  121. * <p> If the first byte cannot be read for any reason other than end of
  122. * file, then an <code>IOException</code> is thrown. In particular, an
  123. * <code>IOException</code> is thrown if the input stream has been closed.
  124. *
  125. * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method
  126. * for class <code>InputStream</code> simply calls the method
  127. * <code>read()</code> repeatedly. If the first such call results in an
  128. * <code>IOException</code>, that exception is returned from the call to
  129. * the <code>read(b,</code> <code>off,</code> <code>len)</code> method. If
  130. * any subsequent call to <code>read()</code> results in a
  131. * <code>IOException</code>, the exception is caught and treated as if it
  132. * were end of file; the bytes read up to that point are stored into
  133. * <code>b</code> and the number of bytes read before the exception
  134. * occurred is returned. Subclasses are encouraged to provide a more
  135. * efficient implementation of this method.
  136. *
  137. * @param b the buffer into which the data is read.
  138. * @param off the start offset in array <code>b</code>
  139. * at which the data is written.
  140. * @param len the maximum number of bytes to read.
  141. * @return the total number of bytes read into the buffer, or
  142. * <code>-1</code> if there is no more data because the end of
  143. * the stream has been reached.
  144. * @exception IOException if an I/O error occurs.
  145. * @exception NullPointerException if <code>b</code> is <code>null</code>.
  146. * @see java.io.InputStream#read()
  147. */
  148. public int read(byte b[], int off, int len) throws IOException {
  149. if (b == null) {
  150. throw new NullPointerException();
  151. } else if ((off < 0) || (off > b.length) || (len < 0) ||
  152. ((off + len) > b.length) || ((off + len) < 0)) {
  153. throw new IndexOutOfBoundsException();
  154. } else if (len == 0) {
  155. return 0;
  156. }
  157. int c = read();
  158. if (c == -1) {
  159. return -1;
  160. }
  161. b[off] = (byte)c;
  162. int i = 1;
  163. try {
  164. for (; i < len ; i++) {
  165. c = read();
  166. if (c == -1) {
  167. break;
  168. }
  169. if (b != null) {
  170. b[off + i] = (byte)c;
  171. }
  172. }
  173. } catch (IOException ee) {
  174. }
  175. return i;
  176. }
  177. /**
  178. * Skips over and discards <code>n</code> bytes of data from this input
  179. * stream. The <code>skip</code> method may, for a variety of reasons, end
  180. * up skipping over some smaller number of bytes, possibly <code>0</code>.
  181. * This may result from any of a number of conditions; reaching end of file
  182. * before <code>n</code> bytes have been skipped is only one possibility.
  183. * The actual number of bytes skipped is returned. If <code>n</code> is
  184. * negative, no bytes are skipped.
  185. *
  186. * <p> The <code>skip</code> method of <code>InputStream</code> creates a
  187. * byte array and then repeatedly reads into it until <code>n</code> bytes
  188. * have been read or the end of the stream has been reached. Subclasses are
  189. * encouraged to provide a more efficient implementation of this method.
  190. *
  191. * @param n the number of bytes to be skipped.
  192. * @return the actual number of bytes skipped.
  193. * @exception IOException if an I/O error occurs.
  194. */
  195. public long skip(long n) throws IOException {
  196. long remaining = n;
  197. int nr;
  198. if (skipBuffer == null)
  199. skipBuffer = new byte[SKIP_BUFFER_SIZE];
  200. byte[] localSkipBuffer = skipBuffer;
  201. if (n <= 0) {
  202. return 0;
  203. }
  204. while (remaining > 0) {
  205. nr = read(localSkipBuffer, 0,
  206. (int) Math.min(SKIP_BUFFER_SIZE, remaining));
  207. if (nr < 0) {
  208. break;
  209. }
  210. remaining -= nr;
  211. }
  212. return n - remaining;
  213. }
  214. /**
  215. * Returns the number of bytes that can be read (or skipped over) from
  216. * this input stream without blocking by the next caller of a method for
  217. * this input stream. The next caller might be the same thread or or
  218. * another thread.
  219. *
  220. * <p> The <code>available</code> method for class <code>InputStream</code>
  221. * always returns <code>0</code>.
  222. *
  223. * <p> This method should be overridden by subclasses.
  224. *
  225. * @return the number of bytes that can be read from this input stream
  226. * without blocking.
  227. * @exception IOException if an I/O error occurs.
  228. */
  229. public int available() throws IOException {
  230. return 0;
  231. }
  232. /**
  233. * Closes this input stream and releases any system resources associated
  234. * with the stream.
  235. *
  236. * <p> The <code>close</code> method of <code>InputStream</code> does
  237. * nothing.
  238. *
  239. * @exception IOException if an I/O error occurs.
  240. */
  241. public void close() throws IOException {}
  242. /**
  243. * Marks the current position in this input stream. A subsequent call to
  244. * the <code>reset</code> method repositions this stream at the last marked
  245. * position so that subsequent reads re-read the same bytes.
  246. *
  247. * <p> The <code>readlimit</code> arguments tells this input stream to
  248. * allow that many bytes to be read before the mark position gets
  249. * invalidated.
  250. *
  251. * <p> The general contract of <code>mark</code> is that, if the method
  252. * <code>markSupported</code> returns <code>true</code>, the stream somehow
  253. * remembers all the bytes read after the call to <code>mark</code> and
  254. * stands ready to supply those same bytes again if and whenever the method
  255. * <code>reset</code> is called. However, the stream is not required to
  256. * remember any data at all if more than <code>readlimit</code> bytes are
  257. * read from the stream before <code>reset</code> is called.
  258. *
  259. * <p> The <code>mark</code> method of <code>InputStream</code> does
  260. * nothing.
  261. *
  262. * @param readlimit the maximum limit of bytes that can be read before
  263. * the mark position becomes invalid.
  264. * @see java.io.InputStream#reset()
  265. */
  266. public synchronized void mark(int readlimit) {}
  267. /**
  268. * Repositions this stream to the position at the time the
  269. * <code>mark</code> method was last called on this input stream.
  270. *
  271. * <p> The general contract of <code>reset</code> is:
  272. *
  273. * <p><ul>
  274. *
  275. * <li> If the method <code>markSupported</code> returns
  276. * <code>true</code>, then:
  277. *
  278. * <ul><li> If the method <code>mark</code> has not been called since
  279. * the stream was created, or the number of bytes read from the stream
  280. * since <code>mark</code> was last called is larger than the argument
  281. * to <code>mark</code> at that last call, then an
  282. * <code>IOException</code> might be thrown.
  283. *
  284. * <li> If such an <code>IOException</code> is not thrown, then the
  285. * stream is reset to a state such that all the bytes read since the
  286. * most recent call to <code>mark</code> (or since the start of the
  287. * file, if <code>mark</code> has not been called) will be resupplied
  288. * to subsequent callers of the <code>read</code> method, followed by
  289. * any bytes that otherwise would have been the next input data as of
  290. * the time of the call to <code>reset</code>. </ul>
  291. *
  292. * <li> If the method <code>markSupported</code> returns
  293. * <code>false</code>, then:
  294. *
  295. * <ul><li> The call to <code>reset</code> may throw an
  296. * <code>IOException</code>.
  297. *
  298. * <li> If an <code>IOException</code> is not thrown, then the stream
  299. * is reset to a fixed state that depends on the particular type of the
  300. * input stream and how it was created. The bytes that will be supplied
  301. * to subsequent callers of the <code>read</code> method depend on the
  302. * particular type of the input stream. </ul></ul>
  303. *
  304. * <p> The method <code>reset</code> for class <code>InputStream</code>
  305. * does nothing and always throws an <code>IOException</code>.
  306. *
  307. * @exception IOException if this stream has not been marked or if the
  308. * mark has been invalidated.
  309. * @see java.io.InputStream#mark(int)
  310. * @see java.io.IOException
  311. */
  312. public synchronized void reset() throws IOException {
  313. throw new IOException("mark/reset not supported");
  314. }
  315. /**
  316. * Tests if this input stream supports the <code>mark</code> and
  317. * <code>reset</code> methods. Whether or not <code>mark</code> and
  318. * <code>reset</code> are supported is an invariant property of a
  319. * particular input stream instance. The <code>markSupported</code> method
  320. * of <code>InputStream</code> returns <code>false</code>.
  321. *
  322. * @return <code>true</code> if this stream instance supports the mark
  323. * and reset methods; <code>false</code> otherwise.
  324. * @see java.io.InputStream#mark(int)
  325. * @see java.io.InputStream#reset()
  326. */
  327. public boolean markSupported() {
  328. return false;
  329. }
  330. }