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