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