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