BufferedReader.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /* BufferedReader.java
  2. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.io;
  33. import gnu.java.lang.CPStringBuilder;
  34. /* Written using "Java Class Libraries", 2nd edition, plus online
  35. * API docs for JDK 1.2 beta from http://www.javasoft.com.
  36. * Status: Believed complete and correct.
  37. */
  38. /**
  39. * This subclass of <code>FilterReader</code> buffers input from an
  40. * underlying implementation to provide a possibly more efficient read
  41. * mechanism. It maintains the buffer and buffer state in instance
  42. * variables that are available to subclasses. The default buffer size
  43. * of 8192 chars can be overridden by the creator of the stream.
  44. * <p>
  45. * This class also implements mark/reset functionality. It is capable
  46. * of remembering any number of input chars, to the limits of
  47. * system memory or the size of <code>Integer.MAX_VALUE</code>
  48. *
  49. * @author Per Bothner (bothner@cygnus.com)
  50. * @author Aaron M. Renn (arenn@urbanophile.com)
  51. */
  52. public class BufferedReader extends Reader
  53. {
  54. Reader in;
  55. char[] buffer;
  56. /* Index of current read position. Must be >= 0 and <= limit. */
  57. /* There is a special case where pos may be equal to limit+1; this
  58. * is used as an indicator that a readLine was done with a '\r' was
  59. * the very last char in the buffer. Since we don't want to read-ahead
  60. * and potentially block, we set pos this way to indicate the situation
  61. * and deal with it later. Doing it this way rather than having a
  62. * separate boolean field to indicate the condition has the advantage
  63. * that it is self-clearing on things like mark/reset.
  64. */
  65. int pos;
  66. /* Limit of valid data in buffer. Must be >= pos and <= buffer.length. */
  67. /* This can be < pos in the one special case described above. */
  68. int limit;
  69. /* The value -1 means there is no mark, or the mark has been invalidated.
  70. Otherwise, markPos is the index in the buffer of the marked position.
  71. Must be >= 0 and <= pos.
  72. Note we do not explicitly store the read-limit.
  73. The implicit read-limit is (buffer.length - markPos), which is
  74. guaranteed to be >= the read-limit requested in the call to mark. */
  75. int markPos = -1;
  76. // The JCL book specifies the default buffer size as 8K characters.
  77. // This is package-private because it is used by LineNumberReader.
  78. static final int DEFAULT_BUFFER_SIZE = 8192;
  79. /**
  80. * Create a new <code>BufferedReader</code> that will read from the
  81. * specified subordinate stream with a default buffer size of 8192 chars.
  82. *
  83. * @param in The subordinate stream to read from
  84. */
  85. public BufferedReader(Reader in)
  86. {
  87. this(in, DEFAULT_BUFFER_SIZE);
  88. }
  89. /**
  90. * Create a new <code>BufferedReader</code> that will read from the
  91. * specified subordinate stream with a buffer size that is specified by the
  92. * caller.
  93. *
  94. * @param in The subordinate stream to read from
  95. * @param size The buffer size to use
  96. *
  97. * @exception IllegalArgumentException if size &lt;= 0
  98. */
  99. public BufferedReader(Reader in, int size)
  100. {
  101. super(in.lock);
  102. if (size <= 0)
  103. throw new IllegalArgumentException("Illegal buffer size: " + size);
  104. this.in = in;
  105. buffer = new char[size];
  106. }
  107. /**
  108. * This method closes the underlying stream and frees any associated
  109. * resources.
  110. *
  111. * @exception IOException If an error occurs
  112. */
  113. public void close() throws IOException
  114. {
  115. synchronized (lock)
  116. {
  117. if (in != null)
  118. in.close();
  119. in = null;
  120. buffer = null;
  121. }
  122. }
  123. /**
  124. * Returns <code>true</code> to indicate that this class supports mark/reset
  125. * functionality.
  126. *
  127. * @return <code>true</code>
  128. */
  129. public boolean markSupported()
  130. {
  131. return true;
  132. }
  133. /**
  134. * Mark a position in the input to which the stream can be
  135. * "reset" by calling the <code>reset()</code> method. The parameter
  136. * <code>readLimit</code> is the number of chars that can be read from the
  137. * stream after setting the mark before the mark becomes invalid. For
  138. * example, if <code>mark()</code> is called with a read limit of 10, then
  139. * when 11 chars of data are read from the stream before the
  140. * <code>reset()</code> method is called, then the mark is invalid and the
  141. * stream object instance is not required to remember the mark.
  142. * <p>
  143. * Note that the number of chars that can be remembered by this method
  144. * can be greater than the size of the internal read buffer. It is also
  145. * not dependent on the subordinate stream supporting mark/reset
  146. * functionality.
  147. *
  148. * @param readLimit The number of chars that can be read before the mark
  149. * becomes invalid
  150. *
  151. * @exception IOException If an error occurs
  152. * @exception IllegalArgumentException if readLimit is negative.
  153. */
  154. public void mark(int readLimit) throws IOException
  155. {
  156. if (readLimit < 0)
  157. throw new IllegalArgumentException("Read-ahead limit is negative");
  158. synchronized (lock)
  159. {
  160. checkStatus();
  161. // In this method we need to be aware of the special case where
  162. // pos + 1 == limit. This indicates that a '\r' was the last char
  163. // in the buffer during a readLine. We'll want to maintain that
  164. // condition after we shift things around and if a larger buffer is
  165. // needed to track readLimit, we'll have to make it one element
  166. // larger to ensure we don't invalidate the mark too early, if the
  167. // char following the '\r' is NOT a '\n'. This is ok because, per
  168. // the spec, we are not required to invalidate when passing readLimit.
  169. //
  170. // Note that if 'pos > limit', then doing 'limit -= pos' will cause
  171. // limit to be negative. This is the only way limit will be < 0.
  172. if (pos + readLimit > limit)
  173. {
  174. char[] old_buffer = buffer;
  175. int extraBuffSpace = 0;
  176. if (pos > limit)
  177. extraBuffSpace = 1;
  178. if (readLimit + extraBuffSpace > limit)
  179. buffer = new char[readLimit + extraBuffSpace];
  180. limit -= pos;
  181. if (limit >= 0)
  182. {
  183. System.arraycopy(old_buffer, pos, buffer, 0, limit);
  184. pos = 0;
  185. }
  186. }
  187. if (limit < 0)
  188. {
  189. // Maintain the relationship of 'pos > limit'.
  190. pos = 1;
  191. limit = markPos = 0;
  192. }
  193. else
  194. markPos = pos;
  195. // Now pos + readLimit <= buffer.length. thus if we need to read
  196. // beyond buffer.length, then we are allowed to invalidate markPos.
  197. }
  198. }
  199. /**
  200. * Reset the stream to the point where the <code>mark()</code> method
  201. * was called. Any chars that were read after the mark point was set will
  202. * be re-read during subsequent reads.
  203. * <p>
  204. * This method will throw an IOException if the number of chars read from
  205. * the stream since the call to <code>mark()</code> exceeds the mark limit
  206. * passed when establishing the mark.
  207. *
  208. * @exception IOException If an error occurs;
  209. */
  210. public void reset() throws IOException
  211. {
  212. synchronized (lock)
  213. {
  214. checkStatus();
  215. if (markPos < 0)
  216. throw new IOException("mark never set or invalidated");
  217. // Need to handle the extremely unlikely case where a readLine was
  218. // done with a '\r' as the last char in the buffer; which was then
  219. // immediately followed by a mark and a reset with NO intervening
  220. // read of any sort. In that case, setting pos to markPos would
  221. // lose that info and a subsequent read would thus not skip a '\n'
  222. // (if one exists). The value of limit in this rare case is zero.
  223. // We can assume that if limit is zero for other reasons, then
  224. // pos is already set to zero and doesn't need to be readjusted.
  225. if (limit > 0)
  226. pos = markPos;
  227. }
  228. }
  229. /**
  230. * This method determines whether or not a stream is ready to be read. If
  231. * this method returns <code>false</code> then this stream could (but is
  232. * not guaranteed to) block on the next read attempt.
  233. *
  234. * @return <code>true</code> if this stream is ready to be read,
  235. * <code>false</code> otherwise
  236. *
  237. * @exception IOException If an error occurs
  238. */
  239. public boolean ready() throws IOException
  240. {
  241. synchronized (lock)
  242. {
  243. checkStatus();
  244. return pos < limit || in.ready();
  245. }
  246. }
  247. /**
  248. * This method read chars from a stream and stores them into a caller
  249. * supplied buffer. It starts storing the data at index
  250. * <code>offset</code> into
  251. * the buffer and attempts to read <code>len</code> chars. This method can
  252. * return before reading the number of chars requested. The actual number
  253. * of chars read is returned as an int. A -1 is returned to indicate the
  254. * end of the stream.
  255. * <p>
  256. * This method will block until some data can be read.
  257. *
  258. * @param buf The array into which the chars read should be stored
  259. * @param offset The offset into the array to start storing chars
  260. * @param count The requested number of chars to read
  261. *
  262. * @return The actual number of chars read, or -1 if end of stream.
  263. *
  264. * @exception IOException If an error occurs.
  265. * @exception IndexOutOfBoundsException If offset and count are not
  266. * valid regarding buf.
  267. */
  268. public int read(char[] buf, int offset, int count) throws IOException
  269. {
  270. if (offset < 0 || offset + count > buf.length || count < 0)
  271. throw new IndexOutOfBoundsException();
  272. synchronized (lock)
  273. {
  274. checkStatus();
  275. // Once again, we need to handle the special case of a readLine
  276. // that has a '\r' at the end of the buffer. In this case, we'll
  277. // need to skip a '\n' if it is the next char to be read.
  278. // This special case is indicated by 'pos > limit'.
  279. boolean retAtEndOfBuffer = false;
  280. int avail = limit - pos;
  281. if (count > avail)
  282. {
  283. if (avail > 0)
  284. count = avail;
  285. else // pos >= limit
  286. {
  287. if (limit == buffer.length)
  288. markPos = -1; // read too far - invalidate the mark.
  289. if (pos > limit)
  290. {
  291. // Set a boolean and make pos == limit to simplify things.
  292. retAtEndOfBuffer = true;
  293. --pos;
  294. }
  295. if (markPos < 0)
  296. {
  297. // Optimization: can read directly into buf.
  298. if (count >= buffer.length && !retAtEndOfBuffer)
  299. return in.read(buf, offset, count);
  300. pos = limit = 0;
  301. }
  302. avail = in.read(buffer, limit, buffer.length - limit);
  303. if (retAtEndOfBuffer && avail > 0 && buffer[limit] == '\n')
  304. {
  305. --avail;
  306. limit++;
  307. }
  308. if (avail < count)
  309. {
  310. if (avail <= 0)
  311. return avail;
  312. count = avail;
  313. }
  314. limit += avail;
  315. }
  316. }
  317. System.arraycopy(buffer, pos, buf, offset, count);
  318. pos += count;
  319. return count;
  320. }
  321. }
  322. /* Read more data into the buffer. Update pos and limit appropriately.
  323. Assumes pos==limit initially. May invalidate the mark if read too much.
  324. Return number of chars read (never 0), or -1 on eof. */
  325. private int fill() throws IOException
  326. {
  327. checkStatus();
  328. // Handle the special case of a readLine that has a '\r' at the end of
  329. // the buffer. In this case, we'll need to skip a '\n' if it is the
  330. // next char to be read. This special case is indicated by 'pos > limit'.
  331. boolean retAtEndOfBuffer = false;
  332. if (pos > limit)
  333. {
  334. retAtEndOfBuffer = true;
  335. --pos;
  336. }
  337. if (markPos >= 0 && limit == buffer.length)
  338. markPos = -1;
  339. if (markPos < 0)
  340. pos = limit = 0;
  341. int count = in.read(buffer, limit, buffer.length - limit);
  342. if (count > 0)
  343. limit += count;
  344. if (retAtEndOfBuffer && buffer[pos] == '\n')
  345. {
  346. --count;
  347. // If the mark was set to the location of the \n, then we
  348. // must change it to fully pretend that the \n does not
  349. // exist.
  350. if (markPos == pos)
  351. ++markPos;
  352. ++pos;
  353. }
  354. return count;
  355. }
  356. public int read() throws IOException
  357. {
  358. synchronized (lock)
  359. {
  360. checkStatus();
  361. if (pos >= limit && fill () <= 0)
  362. return -1;
  363. return buffer[pos++];
  364. }
  365. }
  366. /* Return the end of the line starting at this.pos and ending at limit.
  367. * The index returns is *before* any line terminators, or limit
  368. * if no line terminators were found.
  369. */
  370. private int lineEnd(int limit)
  371. {
  372. int i = pos;
  373. for (; i < limit; i++)
  374. {
  375. char ch = buffer[i];
  376. if (ch == '\n' || ch == '\r')
  377. break;
  378. }
  379. return i;
  380. }
  381. /**
  382. * This method reads a single line of text from the input stream, returning
  383. * it as a <code>String</code>. A line is terminated by "\n", a "\r", or
  384. * an "\r\n" sequence. The system dependent line separator is not used.
  385. * The line termination characters are not returned in the resulting
  386. * <code>String</code>.
  387. *
  388. * @return The line of text read, or <code>null</code> if end of stream.
  389. *
  390. * @exception IOException If an error occurs
  391. */
  392. public String readLine() throws IOException
  393. {
  394. checkStatus();
  395. // Handle the special case where a previous readLine (with no intervening
  396. // reads/skips) had a '\r' at the end of the buffer.
  397. // In this case, we'll need to skip a '\n' if it's the next char to be read.
  398. // This special case is indicated by 'pos > limit'.
  399. if (pos > limit)
  400. {
  401. int ch = read();
  402. if (ch < 0)
  403. return null;
  404. if (ch != '\n')
  405. --pos;
  406. }
  407. int i = lineEnd(limit);
  408. if (i < limit)
  409. {
  410. String str = String.valueOf(buffer, pos, i - pos);
  411. pos = i + 1;
  412. // If the last char in the buffer is a '\r', we must remember
  413. // to check if the next char to be read after the buffer is refilled
  414. // is a '\n'. If so, skip it. To indicate this condition, we set pos
  415. // to be limit + 1, which normally is never possible.
  416. if (buffer[i] == '\r')
  417. if (pos == limit || buffer[pos] == '\n')
  418. pos++;
  419. return str;
  420. }
  421. CPStringBuilder sbuf = new CPStringBuilder(200);
  422. sbuf.append(buffer, pos, i - pos);
  423. pos = i;
  424. // We only want to return null when no characters were read before
  425. // EOF. So we must keep track of this separately. Otherwise we
  426. // would treat an empty `sbuf' as an EOF condition, which is wrong
  427. // when there is just a newline.
  428. boolean eof = false;
  429. for (;;)
  430. {
  431. // readLine should block. So we must not return until a -1 is reached.
  432. if (pos >= limit)
  433. {
  434. // here count == 0 isn't sufficient to give a failure.
  435. int count = fill();
  436. if (count < 0)
  437. {
  438. eof = true;
  439. break;
  440. }
  441. continue;
  442. }
  443. int ch = buffer[pos++];
  444. if (ch == '\n' || ch == '\r')
  445. {
  446. // Check here if a '\r' was the last char in the buffer; if so,
  447. // mark it as in the comment above to indicate future reads
  448. // should skip a newline that is the next char read after
  449. // refilling the buffer.
  450. if (ch == '\r')
  451. if (pos == limit || buffer[pos] == '\n')
  452. pos++;
  453. break;
  454. }
  455. i = lineEnd(limit);
  456. sbuf.append(buffer, pos - 1, i - (pos - 1));
  457. pos = i;
  458. }
  459. return (sbuf.length() == 0 && eof) ? null : sbuf.toString();
  460. }
  461. /**
  462. * This method skips the specified number of chars in the stream. It
  463. * returns the actual number of chars skipped, which may be less than the
  464. * requested amount.
  465. * <p>
  466. * This method first discards chars in the buffer, then calls the
  467. * <code>skip</code> method on the underlying stream to skip the
  468. * remaining chars.
  469. *
  470. * @param count The requested number of chars to skip
  471. *
  472. * @return The actual number of chars skipped.
  473. *
  474. * @exception IOException If an error occurs.
  475. * @exception IllegalArgumentException If count is negative.
  476. */
  477. public long skip(long count) throws IOException
  478. {
  479. synchronized (lock)
  480. {
  481. checkStatus();
  482. if (count < 0)
  483. throw new IllegalArgumentException("skip value is negative");
  484. if (count == 0)
  485. return 0;
  486. // Yet again, we need to handle the special case of a readLine
  487. // that has a '\r' at the end of the buffer. In this case, we need
  488. // to ignore a '\n' if it is the next char to be read.
  489. // This special case is indicated by 'pos > limit' (i.e. avail < 0).
  490. // To simplify things, if we're dealing with the special case for
  491. // readLine, just read the next char (since the fill method will
  492. // skip the '\n' for us). By doing this, we'll have to back up pos.
  493. // That's easier than trying to keep track of whether we've skipped
  494. // one element or not.
  495. if (pos > limit)
  496. {
  497. if (read() < 0)
  498. return 0;
  499. else
  500. --pos;
  501. }
  502. int avail = limit - pos;
  503. if (count < avail)
  504. {
  505. pos += count;
  506. return count;
  507. }
  508. pos = limit;
  509. long todo = count - avail;
  510. if (todo > buffer.length)
  511. {
  512. markPos = -1;
  513. todo -= in.skip(todo);
  514. }
  515. else
  516. {
  517. while (todo > 0)
  518. {
  519. avail = fill();
  520. if (avail <= 0)
  521. break;
  522. if (avail > todo)
  523. avail = (int) todo;
  524. pos += avail;
  525. todo -= avail;
  526. }
  527. }
  528. return count - todo;
  529. }
  530. }
  531. private void checkStatus() throws IOException
  532. {
  533. if (in == null)
  534. throw new IOException("Stream closed");
  535. }
  536. }