1. /*
  2. * @(#)BufferedInputStream.java 1.41 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. * A <code>BufferedInputStream</code> adds
  13. * functionality to another input stream-namely,
  14. * the ability to buffer the input and to
  15. * support the <code>mark</code> and <code>reset</code>
  16. * methods. When the <code>BufferedInputStream</code>
  17. * is created, an internal buffer array is
  18. * created. As bytes from the stream are read
  19. * or skipped, the internal buffer is refilled
  20. * as necessary from the contained input stream,
  21. * many bytes at a time. The <code>mark</code>
  22. * operation remembers a point in the input
  23. * stream and the <code>reset</code> operation
  24. * causes all the bytes read since the most
  25. * recent <code>mark</code> operation to be
  26. * reread before new bytes are taken from
  27. * the contained input stream.
  28. *
  29. * @author Arthur van Hoff
  30. * @version 1.41, 02/02/00
  31. * @since JDK1.0
  32. */
  33. public
  34. class BufferedInputStream extends FilterInputStream {
  35. private static int defaultBufferSize = 2048;
  36. /**
  37. * The internal buffer array where the data is stored. When necessary,
  38. * it may be replaced by another array of
  39. * a different size.
  40. */
  41. protected byte buf[];
  42. /**
  43. * The index one greater than the index of the last valid byte in
  44. * the buffer.
  45. * This value is always
  46. * in the range <code>0</code> through <code>buf.length</code>
  47. * elements <code>buf[0]</code> through <code>buf[count-1]
  48. * </code>contain buffered input data obtained
  49. * from the underlying input stream.
  50. */
  51. protected int count;
  52. /**
  53. * The current position in the buffer. This is the index of the next
  54. * character to be read from the <code>buf</code> array.
  55. * <p>
  56. * This value is always in the range <code>0</code>
  57. * through <code>count</code>. If it is less
  58. * than <code>count</code>, then <code>buf[pos]</code>
  59. * is the next byte to be supplied as input;
  60. * if it is equal to <code>count</code>, then
  61. * the next <code>read</code> or <code>skip</code>
  62. * operation will require more bytes to be
  63. * read from the contained input stream.
  64. *
  65. * @see java.io.BufferedInputStream#buf
  66. */
  67. protected int pos;
  68. /**
  69. * The value of the <code>pos</code> field at the time the last
  70. * <code>mark</code> method was called.
  71. * <p>
  72. * This value is always
  73. * in the range <code>-1</code> through <code>pos</code>.
  74. * If there is no marked position in the input
  75. * stream, this field is <code>-1</code>. If
  76. * there is a marked position in the input
  77. * stream, then <code>buf[markpos]</code>
  78. * is the first byte to be supplied as input
  79. * after a <code>reset</code> operation. If
  80. * <code>markpos</code> is not <code>-1</code>,
  81. * then all bytes from positions <code>buf[markpos]</code>
  82. * through <code>buf[pos-1]</code> must remain
  83. * in the buffer array (though they may be
  84. * moved to another place in the buffer array,
  85. * with suitable adjustments to the values
  86. * of <code>count</code>, <code>pos</code>,
  87. * and <code>markpos</code>); they may not
  88. * be discarded unless and until the difference
  89. * between <code>pos</code> and <code>markpos</code>
  90. * exceeds <code>marklimit</code>.
  91. *
  92. * @see java.io.BufferedInputStream#mark(int)
  93. * @see java.io.BufferedInputStream#pos
  94. */
  95. protected int markpos = -1;
  96. /**
  97. * The maximum read ahead allowed after a call to the
  98. * <code>mark</code> method before subsequent calls to the
  99. * <code>reset</code> method fail.
  100. * Whenever the difference between <code>pos</code>
  101. * and <code>markpos</code> exceeds <code>marklimit</code>,
  102. * then the mark may be dropped by setting
  103. * <code>markpos</code> to <code>-1</code>.
  104. *
  105. * @see java.io.BufferedInputStream#mark(int)
  106. * @see java.io.BufferedInputStream#reset()
  107. */
  108. protected int marklimit;
  109. /**
  110. * Check to make sure that this stream has not been closed
  111. */
  112. private void ensureOpen() throws IOException {
  113. if (in == null)
  114. throw new IOException("Stream closed");
  115. }
  116. /**
  117. * Creates a <code>BufferedInputStream</code>
  118. * and saves its argument, the input stream
  119. * <code>in</code>, for later use. An internal
  120. * buffer array is created and stored in <code>buf</code>.
  121. *
  122. * @param in the underlying input stream.
  123. */
  124. public BufferedInputStream(InputStream in) {
  125. this(in, defaultBufferSize);
  126. }
  127. /**
  128. * Creates a <code>BufferedInputStream</code>
  129. * with the specified buffer size,
  130. * and saves its argument, the input stream
  131. * <code>in</code>, for later use. An internal
  132. * buffer array of length <code>size</code>
  133. * is created and stored in <code>buf</code>.
  134. *
  135. * @param in the underlying input stream.
  136. * @param size the buffer size.
  137. * @exception IllegalArgumentException if size <= 0.
  138. */
  139. public BufferedInputStream(InputStream in, int size) {
  140. super(in);
  141. if (size <= 0) {
  142. throw new IllegalArgumentException("Buffer size <= 0");
  143. }
  144. buf = new byte[size];
  145. }
  146. /**
  147. * Fills the buffer with more data, taking into account
  148. * shuffling and other tricks for dealing with marks.
  149. * Assumes that it is being called by a synchronized method.
  150. * This method also assumes that all data has already been read in,
  151. * hence pos > count.
  152. */
  153. private void fill() throws IOException {
  154. if (markpos < 0)
  155. pos = 0; /* no mark: throw away the buffer */
  156. else if (pos >= buf.length) /* no room left in buffer */
  157. if (markpos > 0) { /* can throw away early part of the buffer */
  158. int sz = pos - markpos;
  159. System.arraycopy(buf, markpos, buf, 0, sz);
  160. pos = sz;
  161. markpos = 0;
  162. } else if (buf.length >= marklimit) {
  163. markpos = -1; /* buffer got too big, invalidate mark */
  164. pos = 0; /* drop buffer contents */
  165. } else { /* grow buffer */
  166. int nsz = pos * 2;
  167. if (nsz > marklimit)
  168. nsz = marklimit;
  169. byte nbuf[] = new byte[nsz];
  170. System.arraycopy(buf, 0, nbuf, 0, pos);
  171. buf = nbuf;
  172. }
  173. count = pos;
  174. int n = in.read(buf, pos, buf.length - pos);
  175. if (n > 0)
  176. count = n + pos;
  177. }
  178. /**
  179. * See
  180. * the general contract of the <code>read</code>
  181. * method of <code>InputStream</code>.
  182. *
  183. * @return the next byte of data, or <code>-1</code> if the end of the
  184. * stream is reached.
  185. * @exception IOException if an I/O error occurs.
  186. * @see java.io.FilterInputStream#in
  187. */
  188. public synchronized int read() throws IOException {
  189. ensureOpen();
  190. if (pos >= count) {
  191. fill();
  192. if (pos >= count)
  193. return -1;
  194. }
  195. return buf[pos++] & 0xff;
  196. }
  197. /**
  198. * Read characters into a portion of an array, reading from the underlying
  199. * stream at most once if necessary.
  200. */
  201. private int read1(byte[] b, int off, int len) throws IOException {
  202. int avail = count - pos;
  203. if (avail <= 0) {
  204. /* If the requested length is at least as large as the buffer, and
  205. if there is no mark/reset activity, do not bother to copy the
  206. bytes into the local buffer. In this way buffered streams will
  207. cascade harmlessly. */
  208. if (len >= buf.length && markpos < 0) {
  209. return in.read(b, off, len);
  210. }
  211. fill();
  212. avail = count - pos;
  213. if (avail <= 0) return -1;
  214. }
  215. int cnt = (avail < len) ? avail : len;
  216. System.arraycopy(buf, pos, b, off, cnt);
  217. pos += cnt;
  218. return cnt;
  219. }
  220. /**
  221. * Reads bytes from this byte-input stream into the specified byte array,
  222. * starting at the given offset.
  223. *
  224. * <p> This method implements the general contract of the corresponding
  225. * <code>{@link InputStream#read(byte[], int, int) read}</code> method of
  226. * the <code>{@link InputStream}</code> class. As an additional
  227. * convenience, it attempts to read as many bytes as possible by repeatedly
  228. * invoking the <code>read</code> method of the underlying stream. This
  229. * iterated <code>read</code> continues until one of the following
  230. * conditions becomes true: <ul>
  231. *
  232. * <li> The specified number of bytes have been read,
  233. *
  234. * <li> The <code>read</code> method of the underlying stream returns
  235. * <code>-1</code>, indicating end-of-file, or
  236. *
  237. * <li> The <code>available</code> method of the underlying stream
  238. * returns zero, indicating that further input requests would block.
  239. *
  240. * </ul> If the first <code>read</code> on the underlying stream returns
  241. * <code>-1</code> to indicate end-of-file then this method returns
  242. * <code>-1</code>. Otherwise this method returns the number of bytes
  243. * actually read.
  244. *
  245. * <p> Subclasses of this class are encouraged, but not required, to
  246. * attempt to read as many bytes as possible in the same fashion.
  247. *
  248. * @param b destination buffer.
  249. * @param off offset at which to start storing bytes.
  250. * @param len maximum number of bytes to read.
  251. * @return the number of bytes read, or <code>-1</code> if the end of
  252. * the stream has been reached.
  253. * @exception IOException if an I/O error occurs.
  254. */
  255. public synchronized int read(byte b[], int off, int len)
  256. throws IOException
  257. {
  258. ensureOpen();
  259. if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
  260. throw new IndexOutOfBoundsException();
  261. } else if (len == 0) {
  262. return 0;
  263. }
  264. int n = read1(b, off, len);
  265. if (n <= 0) return n;
  266. while ((n < len) && (in.available() > 0)) {
  267. int n1 = read1(b, off + n, len - n);
  268. if (n1 <= 0) break;
  269. n += n1;
  270. }
  271. return n;
  272. }
  273. /**
  274. * See the general contract of the <code>skip</code>
  275. * method of <code>InputStream</code>.
  276. *
  277. * @param n the number of bytes to be skipped.
  278. * @return the actual number of bytes skipped.
  279. * @exception IOException if an I/O error occurs.
  280. */
  281. public synchronized long skip(long n) throws IOException {
  282. ensureOpen();
  283. if (n <= 0) {
  284. return 0;
  285. }
  286. long avail = count - pos;
  287. if (avail <= 0) {
  288. // If no mark position set then don't keep in buffer
  289. if (markpos <0)
  290. return in.skip(n);
  291. // Fill in buffer to save bytes for reset
  292. fill();
  293. avail = count - pos;
  294. if (avail <= 0)
  295. return 0;
  296. }
  297. long skipped = (avail < n) ? avail : n;
  298. pos += skipped;
  299. return skipped;
  300. }
  301. /**
  302. * Returns the number of bytes that can be read from this input
  303. * stream without blocking.
  304. * <p>
  305. * The <code>available</code> method of
  306. * <code>BufferedInputStream</code> returns the sum of the the number
  307. * of bytes remaining to be read in the buffer
  308. * (<code>count - pos</code>)
  309. * and the result of calling the <code>available</code> method of the
  310. * underlying input stream.
  311. *
  312. * @return the number of bytes that can be read from this input
  313. * stream without blocking.
  314. * @exception IOException if an I/O error occurs.
  315. * @see java.io.FilterInputStream#in
  316. */
  317. public synchronized int available() throws IOException {
  318. ensureOpen();
  319. return (count - pos) + in.available();
  320. }
  321. /**
  322. * See the general contract of the <code>mark</code>
  323. * method of <code>InputStream</code>.
  324. *
  325. * @param readlimit the maximum limit of bytes that can be read before
  326. * the mark position becomes invalid.
  327. * @see java.io.BufferedInputStream#reset()
  328. */
  329. public synchronized void mark(int readlimit) {
  330. marklimit = readlimit;
  331. markpos = pos;
  332. }
  333. /**
  334. * See the general contract of the <code>reset</code>
  335. * method of <code>InputStream</code>.
  336. * <p>
  337. * If <code>markpos</code> is <code>-1</code>
  338. * (no mark has been set or the mark has been
  339. * invalidated), an <code>IOException</code>
  340. * is thrown. Otherwise, <code>pos</code> is
  341. * set equal to <code>markpos</code>.
  342. *
  343. * @exception IOException if this stream has not been marked or
  344. * if the mark has been invalidated.
  345. * @see java.io.BufferedInputStream#mark(int)
  346. */
  347. public synchronized void reset() throws IOException {
  348. ensureOpen();
  349. if (markpos < 0)
  350. throw new IOException("Resetting to invalid mark");
  351. pos = markpos;
  352. }
  353. /**
  354. * Tests if this input stream supports the <code>mark</code>
  355. * and <code>reset</code> methods. The <code>markSupported</code>
  356. * method of <code>BufferedInputStream</code> returns
  357. * <code>true</code>.
  358. *
  359. * @return a <code>boolean</code> indicating if this stream type supports
  360. * the <code>mark</code> and <code>reset</code> methods.
  361. * @see java.io.InputStream#mark(int)
  362. * @see java.io.InputStream#reset()
  363. */
  364. public boolean markSupported() {
  365. return true;
  366. }
  367. /**
  368. * Closes this input stream and releases any system resources
  369. * associated with the stream.
  370. *
  371. * @exception IOException if an I/O error occurs.
  372. */
  373. public void close() throws IOException {
  374. if (in == null)
  375. return;
  376. in.close();
  377. in = null;
  378. buf = null;
  379. }
  380. }