LineNumberInputStream.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* LineNumberInputStream.java -- An input stream which counts line numbers
  2. Copyright (C) 1998, 1999, 2002, 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>InputStream</code>
  34. * except that it counts line numbers, and canonicalizes newline
  35. * characters. As data is read, whenever the byte sequences "\r",
  36. * "\n", or "\r\n" are encountered, the running line count is
  37. * incremeted by one. Additionally, the whatever line termination
  38. * sequence was encountered will be converted to a "\n" byte. Note
  39. * that this class numbers lines from 0. When the first line
  40. * terminator is encountered, the line number is incremented to 1, and
  41. * so on.
  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. * <p>
  47. * Note that since this class operates as a filter on an underlying
  48. * stream, it has the same mark/reset functionality as the underlying
  49. * stream. The <code>mark()</code> and <code>reset()</code> methods
  50. * in this class handle line numbers correctly. Calling
  51. * <code>reset()</code> resets the line number to the point at which
  52. * <code>mark()</code> was called if the subordinate stream supports
  53. * that functionality.
  54. * <p>
  55. * @deprecated This class is deprecated in favor if
  56. * <code>LineNumberReader</code> because it operates on ASCII bytes
  57. * instead of an encoded character stream. This class is for backward
  58. * compatibility only and should not be used in new applications.
  59. *
  60. * @author Aaron M. Renn (arenn@urbanophile.com)
  61. * @author Warren Levy (warrenl@cygnus.com)
  62. */
  63. public class LineNumberInputStream extends FilterInputStream
  64. {
  65. /** The current line number. */
  66. private int lineNumber = 0;
  67. /** The line number when the stream was marked. */
  68. private int markLineNumber = 0;
  69. /** Flag to indicate a '\r' was just read so that an immediately
  70. * subsequent '\n' can be ignored. */
  71. private boolean justReadReturnChar = false;
  72. /**
  73. * Create a new <code>LineNumberInputStream</code> that reads from the
  74. * specified subordinate <code>InputStream</code>
  75. *
  76. * @param in The subordinate <code>InputStream</code> to read from
  77. */
  78. public LineNumberInputStream(InputStream in)
  79. {
  80. super(in);
  81. }
  82. /**
  83. * This method returns the number of bytes that can be read from the
  84. * stream before the stream can block. This method is tricky
  85. * because the subordinate <code>InputStream</code> might return
  86. * only "\r\n" characters, which are replaced by a single "\n"
  87. * character by the <code>read()</code> method of this class. So
  88. * this method can only guarantee that <code>in.available() /
  89. * 2</code> bytes can actually be read before blocking. In
  90. * practice, considerably more bytes might be read before blocking
  91. * <p>
  92. * Note that the stream may not block if additional bytes beyond the count
  93. * returned by this method are read.
  94. *
  95. * @return The number of bytes that can be read before blocking could occur
  96. *
  97. * @exception IOException If an error occurs
  98. */
  99. public int available() throws IOException
  100. {
  101. // We can only guarantee half the characters that might be available
  102. // without blocking because "\r\n" is treated as a single character.
  103. return in.available() / 2;
  104. }
  105. /**
  106. * This method returns the current line number
  107. *
  108. * @return The current line number
  109. */
  110. public int getLineNumber()
  111. {
  112. return lineNumber;
  113. }
  114. /**
  115. * This method marks a position in the input to which the stream can
  116. * be "reset" byte calling the <code>reset()</code> method. The
  117. * parameter <code>readlimit</code> is the number of bytes that can
  118. * be read from the stream after setting the mark before the mark
  119. * becomes invalid. For example, if <code>mark()</code> is called
  120. * with a read limit of 10, then when 11 bytes of data are read from
  121. * the stream before the <code>reset()</code> method is called, then
  122. * the mark is invalid and the stream object instance is not
  123. * required to remember the mark.
  124. * <p>
  125. * In this class, this method will remember the current line number
  126. * as well as the current position in the stream. When the
  127. * <code>reset()</code> method is called, the line number will be
  128. * restored to the saved line number in addition to the stream
  129. * position.
  130. * <p>
  131. * This method only works if the subordinate stream supports mark/reset
  132. * functionality.
  133. *
  134. * @param readlimit The number of bytes that can be read before the
  135. * mark becomes invalid
  136. */
  137. public void mark(int readlimit)
  138. {
  139. in.mark(readlimit);
  140. markLineNumber = lineNumber;
  141. }
  142. /**
  143. * This method reads an unsigned byte from the input stream and returns it
  144. * as an int in the range of 0-255. This method will return -1 if the
  145. * end of the stream has been reached.
  146. * <p>
  147. * Note that if a line termination sequence is encountered (ie, "\r",
  148. * "\n", or "\r\n") then that line termination sequence is converted to
  149. * a single "\n" value which is returned from this method. This means
  150. * that it is possible this method reads two bytes from the subordinate
  151. * stream instead of just one.
  152. * <p>
  153. * Note that this method will block until a byte of data is available
  154. * to be read.
  155. *
  156. * @return The byte read or -1 if end of stream
  157. *
  158. * @exception IOException If an error occurs
  159. */
  160. public int read() throws IOException
  161. {
  162. // Treat "\r\n" as a single character. A '\r' may have been read by
  163. // a previous call to read so we keep an internal flag to avoid having
  164. // to read ahead.
  165. int ch = in.read();
  166. if (ch == '\n')
  167. if (justReadReturnChar)
  168. {
  169. ch = in.read();
  170. justReadReturnChar = false;
  171. }
  172. else
  173. lineNumber++;
  174. else if (ch == '\r')
  175. {
  176. ch = '\n';
  177. justReadReturnChar = true;
  178. lineNumber++;
  179. }
  180. else
  181. justReadReturnChar = false;
  182. return ch;
  183. }
  184. /**
  185. * This method reads bytes from a stream and stores them into a caller
  186. * supplied buffer. It starts storing data at index <code>offset</code> into
  187. * the buffer and attemps to read <code>len</code> bytes. This method can
  188. * return before reading the number of bytes requested. The actual number
  189. * of bytes read is returned as an int. A -1 is returned to indicated the
  190. * end of the stream.
  191. * <p>
  192. * This method will block until some data can be read.
  193. * <p>
  194. * Note that if a line termination sequence is encountered (ie, "\r",
  195. * "\n", or "\r\n") then that line termination sequence is converted to
  196. * a single "\n" value which is stored in the buffer. Only a single
  197. * byte is counted towards the number of bytes read in this case.
  198. *
  199. * @param b The array into which the bytes read should be stored
  200. * @param off The offset into the array to start storing bytes
  201. * @param len The requested number of bytes to read
  202. *
  203. * @return The actual number of bytes read, or -1 if end of stream
  204. *
  205. * @exception IOException If an error occurs.
  206. */
  207. public int read(byte[] b, int off, int len) throws IOException
  208. {
  209. if (off < 0 || len < 0 || off + len > b.length)
  210. throw new ArrayIndexOutOfBoundsException();
  211. // This case always succeeds.
  212. if (len == 0)
  213. return 0;
  214. // The simplest, though not necessarily the most time efficient thing
  215. // to do is simply call read(void) len times. Since this is a deprecated
  216. // class, that should be ok.
  217. final int origOff = off;
  218. while (len-- > 0)
  219. {
  220. int ch = read();
  221. if (ch < 0)
  222. break;
  223. b[off++] = (byte) ch;
  224. }
  225. // This is safe since we already know that some bytes were
  226. // actually requested.
  227. return off == origOff ? -1 : off - origOff;
  228. }
  229. /**
  230. * This method resets a stream to the point where the
  231. * <code>mark()</code> method was called. Any bytes that were read
  232. * after the mark point was set will be re-read during subsequent
  233. * reads.
  234. * <p>
  235. * In this class, this method will also restore the line number that was
  236. * current when the <code>mark()</code> method was called.
  237. * <p>
  238. * This method only works if the subordinate stream supports mark/reset
  239. * functionality.
  240. *
  241. * @exception IOException If an error occurs
  242. */
  243. public void reset() throws IOException
  244. {
  245. in.reset();
  246. lineNumber = markLineNumber;
  247. justReadReturnChar = false;
  248. }
  249. /**
  250. * This method sets the current line number to the specified value.
  251. *
  252. * @param lineNumber The new line number
  253. */
  254. public void setLineNumber(int lineNumber)
  255. {
  256. this.lineNumber = lineNumber;
  257. }
  258. /**
  259. * This method skips up to the requested number of bytes in the
  260. * input stream. The actual number of bytes skipped is returned. If the
  261. * desired number of bytes to skip is negative, no bytes are skipped.
  262. *
  263. * @param n requested number of bytes to skip.
  264. *
  265. * @return The actual number of bytes skipped.
  266. *
  267. * @exception IOException If an error occurs.
  268. */
  269. public long skip(long n) throws IOException
  270. {
  271. if (n <= 0)
  272. return 0L;
  273. final long origN = n;
  274. do
  275. {
  276. int ch = read();
  277. if (ch < 0)
  278. break;
  279. if (ch == '\n' || ch == '\r')
  280. lineNumber++;
  281. }
  282. while (--n > 0);
  283. return origN - n;
  284. }
  285. }