LineNumberReader.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* LineNumberReader.java -- A character input stream which counts line numbers
  2. Copyright (C) 1998, 1999, 2001, 2003, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. /**
  33. * This class functions like a standard <code>Reader</code> except that it
  34. * counts line numbers, and canonicalizes newline characters. As data
  35. * is read, whenever the char sequences "\r", "\n", or "\r\n" are encountered,
  36. * the running line count is incremeted by one. Additionally, the whatever
  37. * line termination sequence was encountered will be converted to a "\n"
  38. * char. Note that this class numbers lines from 0. When the first
  39. * line terminator is encountered, the line number is incremented to 1, and
  40. * so on. Also note that actual "\r" and "\n" characters are looked for.
  41. * The system dependent line separator sequence is ignored.
  42. * <p>
  43. * This class counts only line termination characters. If the last line
  44. * read from the stream does not end in a line termination sequence, it
  45. * will not be counted as a line.
  46. *
  47. * @author Per Bothner (bothner@cygnus.com)
  48. * @author Aaron M. Renn (arenn@urbanophile.com)
  49. * @author Guilhem Lavaux (guilhem@kaffe.org)
  50. * @date December 28, 2003.
  51. */
  52. /* Written using "Java Class Libraries", 2nd edition, plus online
  53. * API docs for JDK 1.2 beta from http://www.javasoft.com.
  54. * Status: Believed complete and correct.
  55. *
  56. * This implementation has the feature that if '\r' is read, it
  57. * does not look for a '\n', but immediately returns '\n'.
  58. * On the next read(), if a '\n' is read, it is skipped.
  59. * This has the advantage that we do not read (and hang) unnecessarily.
  60. *
  61. * This implementation is also minimal in the number of fields it uses.
  62. */
  63. public class LineNumberReader extends BufferedReader
  64. {
  65. /** The current line number. */
  66. private int lineNumber;
  67. /** Whether we already found a new line in the former call. */
  68. private boolean matchedNewLine;
  69. /** The saved line number when calling mark() */
  70. private int savedLineNumber;
  71. /**
  72. * Create a new <code>LineNumberReader</code> that reads from the
  73. * specified subordinate <code>Reader</code>. A default 8K char sized
  74. * buffer will be used for reads.
  75. *
  76. * @param in The subordinate <code>Reader</code> to read from
  77. */
  78. public LineNumberReader(Reader in)
  79. {
  80. super(in, DEFAULT_BUFFER_SIZE);
  81. }
  82. /**
  83. * This method initializes a new <code>LineNumberReader</code> to read
  84. * from the specified subordinate <code>Reader</code> using the specified
  85. * read buffer size.
  86. *
  87. * @param in The subordinate <code>Reader</code> to read from
  88. * @param size The buffer size to use for reading
  89. */
  90. public LineNumberReader(Reader in, int size)
  91. {
  92. super(in, size);
  93. }
  94. /**
  95. * This method returns the current line number
  96. *
  97. * @return The current line number
  98. */
  99. public int getLineNumber()
  100. {
  101. return lineNumber;
  102. }
  103. /**
  104. * This method sets the current line number to the specified value.
  105. *
  106. * @param lineNumber The new line number
  107. */
  108. public void setLineNumber(int lineNumber)
  109. {
  110. this.lineNumber = lineNumber;
  111. }
  112. /**
  113. * This method marks a position in the input to which the stream can be
  114. * "reset" char calling the <code>reset()</code> method. The parameter
  115. * <code>readlimit</code> is the number of chars that can be read from the
  116. * stream after setting the mark before the mark becomes invalid. For
  117. * example, if <code>mark()</code> is called with a read limit of 10,
  118. * then when
  119. * 11 chars of data are read from the stream before the <code>reset()</code>
  120. * method is called, then the mark is invalid and the stream object
  121. * instance is not required to remember the mark.
  122. * <p>
  123. * In this class, this method will remember the current line number as well
  124. * as the current position in the stream. When the <code>reset()</code>
  125. * method
  126. * is called, the line number will be restored to the saved line number in
  127. * addition to the stream position.
  128. *
  129. * @param readLimit The number of chars that can be read before the
  130. * mark becomes invalid
  131. *
  132. * @exception IOException If an error occurs
  133. */
  134. public void mark(int readLimit) throws IOException
  135. {
  136. if (readLimit < 0)
  137. throw new IllegalArgumentException("Read-ahead limit is negative");
  138. synchronized (lock)
  139. {
  140. // This is basically the same as BufferedReader.mark.
  141. // However, if the previous character was a '\r', we need to
  142. // save that 'r', in case the next character is a '\n'.
  143. if (pos + readLimit > limit)
  144. {
  145. int saveCR = matchedNewLine ? 1 : 0;
  146. char[] old_buffer = buffer;
  147. if (readLimit > limit)
  148. buffer = new char[saveCR + readLimit];
  149. int copy_start = pos - saveCR;
  150. savedLineNumber = lineNumber;
  151. limit -= copy_start;
  152. System.arraycopy(old_buffer, copy_start, buffer, 0, limit);
  153. pos = saveCR;
  154. }
  155. markPos = pos;
  156. }
  157. }
  158. /**
  159. * This method resets a stream to the point where the <code>mark()</code>
  160. * method
  161. * was called. Any chars that were read after the mark point was set will
  162. * be re-read during subsequent reads.
  163. * <p>
  164. * In this class, this method will also restore the line number that was
  165. * current when the <code>mark()</code> method was called.
  166. *
  167. * @exception IOException If an error occurs
  168. */
  169. public void reset() throws IOException
  170. {
  171. synchronized (lock)
  172. {
  173. if (markPos < 0)
  174. throw new IOException("mark never set or invalidated");
  175. lineNumber = savedLineNumber;
  176. pos = markPos;
  177. matchedNewLine = (markPos > 0 && buffer[markPos-1] == '\r');
  178. }
  179. }
  180. /**
  181. * This private method fills the input buffer whatever pos is.
  182. * Consequently pos should be checked before calling this method.
  183. *
  184. * @return the number of bytes actually read from the input stream or
  185. * -1 if end of stream.
  186. * @exception IOException If an error occurs.
  187. */
  188. private int fill() throws IOException
  189. {
  190. if (markPos >= 0 && limit == buffer.length)
  191. markPos = -1;
  192. if (markPos < 0)
  193. pos = limit = 0;
  194. int count = in.read(buffer, limit, buffer.length - limit);
  195. if (count <= 0)
  196. return -1;
  197. limit += count;
  198. return count;
  199. }
  200. /**
  201. * This method reads an unsigned char from the input stream and returns it
  202. * as an int in the range of 0-65535. This method will return -1 if the
  203. * end of the stream has been reached.
  204. * <p>
  205. * Note that if a line termination sequence is encountered (ie, "\r",
  206. * "\n", or "\r\n") then that line termination sequence is converted to
  207. * a single "\n" value which is returned from this method. This means
  208. * that it is possible this method reads two chars from the subordinate
  209. * stream instead of just one.
  210. * <p>
  211. * Note that this method will block until a char of data is available
  212. * to be read.
  213. *
  214. * @return The char read or -1 if end of stream
  215. *
  216. * @exception IOException If an error occurs
  217. */
  218. public int read() throws IOException
  219. {
  220. synchronized (lock)
  221. {
  222. skipRedundantLF();
  223. if (pos >= limit && fill() < 0)
  224. return -1;
  225. char ch = buffer[pos++];
  226. if ((matchedNewLine = (ch == '\r')) || ch == '\n')
  227. {
  228. lineNumber++;
  229. return '\n';
  230. }
  231. matchedNewLine = false;
  232. return (int) ch;
  233. }
  234. }
  235. /**
  236. * This method reads chars from a stream and stores them into a caller
  237. * supplied buffer. It starts storing data at index <code>offset</code> into
  238. * the buffer and attemps to read <code>len</code> chars. This method can
  239. * return before reading the number of chars requested. The actual number
  240. * of chars read is returned as an int. A -1 is returned to indicated the
  241. * end of the stream.
  242. * <p>
  243. * This method will block until some data can be read.
  244. * <p>
  245. * Note that if a line termination sequence is encountered (ie, "\r",
  246. * "\n", or "\r\n") then that line termination sequence is converted to
  247. * a single "\n" value which is stored in the buffer. Only a single
  248. * char is counted towards the number of chars read in this case.
  249. *
  250. * @param buf The array into which the chars read should be stored
  251. * @param offset The offset into the array to start storing chars
  252. * @param count The requested number of chars to read
  253. *
  254. * @return The actual number of chars read, or -1 if end of stream
  255. *
  256. * @exception IOException If an error occurs.
  257. * @exception NullPointerException If buf is null (in any case).
  258. * @exception IndexOutOfBoundsException If buffer parameters (offset and
  259. * count) lies outside of the buffer capacity.
  260. */
  261. public int read(char[] buf, int offset, int count) throws IOException
  262. {
  263. if (buf == null)
  264. throw new NullPointerException();
  265. if (offset + count > buf.length || offset < 0)
  266. throw new IndexOutOfBoundsException();
  267. if (count <= 0)
  268. {
  269. if (count < 0)
  270. throw new IndexOutOfBoundsException();
  271. return 0;
  272. }
  273. synchronized (lock)
  274. {
  275. if (pos >= limit && fill() < 0)
  276. return -1;
  277. int start_offset = offset;
  278. boolean matched = matchedNewLine;
  279. while (count-- > 0 && pos < limit)
  280. {
  281. char ch = buffer[pos++];
  282. if (ch == '\r')
  283. {
  284. lineNumber++;
  285. matched = true;
  286. }
  287. else if (ch == '\n' && !matched)
  288. lineNumber++;
  289. else
  290. matched = false;
  291. buf[offset++] = ch;
  292. }
  293. matchedNewLine = matched;
  294. return offset - start_offset;
  295. }
  296. }
  297. private void skipRedundantLF() throws IOException
  298. {
  299. if (pos > 0 && matchedNewLine)
  300. {
  301. if (pos < limit)
  302. { // fast case
  303. if (buffer[pos] == '\n')
  304. pos++;
  305. }
  306. else
  307. { // check whether the next buffer begins with '\n'.
  308. // in that case kill the '\n'.
  309. if (fill() <= 0)
  310. return;
  311. if (buffer[pos] == '\n')
  312. pos++;
  313. }
  314. matchedNewLine = true;
  315. }
  316. }
  317. /**
  318. * This method reads a line of text from the input stream and returns
  319. * it as a <code>String</code>. A line is considered to be terminated
  320. * by a "\r", "\n", or "\r\n" sequence, not by the system dependent line
  321. * separator.
  322. *
  323. * @return The line read as a <code>String</code> or <code>null</code>
  324. * if end of stream.
  325. *
  326. * @exception IOException If an error occurs
  327. */
  328. public String readLine() throws IOException
  329. {
  330. // BufferedReader.readLine already does this. Shouldn't need to keep
  331. // track of newlines (since the read method deals with this for us).
  332. // But if the buffer is large, we may not call the read method at all
  333. // and super.readLine can't increment lineNumber itself.
  334. // Though it may seem kludgy, the safest thing to do is to save off
  335. // lineNumber and increment it explicitly when we're done (iff we
  336. // ended with a '\n' or '\r' as opposed to EOF).
  337. //
  338. // Also, we need to undo the special casing done by BufferedReader.readLine
  339. // when a '\r' is the last char in the buffer. That situation is marked
  340. // by 'pos > limit'.
  341. int tmpLineNumber = lineNumber;
  342. skipRedundantLF();
  343. String str = super.readLine();
  344. if (pos > limit)
  345. --pos;
  346. // The only case where you mustn't increment the line number is you are
  347. // at the EOS.
  348. if (str != null)
  349. lineNumber = tmpLineNumber + 1;
  350. return str;
  351. }
  352. /**
  353. * This method skips over characters in the stream. This method will
  354. * skip the specified number of characters if possible, but is not required
  355. * to skip them all. The actual number of characters skipped is returned.
  356. * This method returns 0 if the specified number of chars is less than 1.
  357. *
  358. * @param count The specified number of chars to skip.
  359. *
  360. * @return The actual number of chars skipped.
  361. *
  362. * @exception IOException If an error occurs
  363. */
  364. public long skip (long count) throws IOException
  365. {
  366. if (count < 0)
  367. throw new IllegalArgumentException("skip() value is negative");
  368. if (count == 0)
  369. return 0;
  370. int skipped;
  371. char[] buf = new char[1];
  372. for (skipped = 0; skipped < count; skipped++)
  373. {
  374. int ch = read(buf, 0, 1);
  375. if (ch < 0)
  376. break;
  377. }
  378. return skipped;
  379. }
  380. }