1. /*
  2. * @(#)LineNumberInputStream.java 1.20 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. * This class is an input stream filter that provides the added
  10. * functionality of keeping track of the current line number.
  11. * <p>
  12. * A line is a sequence of bytes ending with a carriage return
  13. * character (<code>'\r'</code>), a newline character
  14. * (<code>'\n'</code>), or a carriage return character followed
  15. * immediately by a linefeed character. In all three cases, the line
  16. * terminating character(s) are returned as a single newline character.
  17. * <p>
  18. * The line number begins at <code>0</code>, and is incremented by
  19. * <code>1</code> when a <code>read</code> returns a newline character.
  20. *
  21. * @author Arthur van Hoff
  22. * @version 1.20, 11/29/01
  23. * @see java.io.LineNumberReader
  24. * @since JDK1.0
  25. * @deprecated This class incorrectly assumes that bytes adequately represent
  26. * characters. As of JDK 1.1, the preferred way to operate on
  27. * character streams is via the new character-stream classes, which
  28. * include a class for counting line numbers.
  29. */
  30. public
  31. class LineNumberInputStream extends FilterInputStream {
  32. int pushBack = -1;
  33. int lineNumber;
  34. int markLineNumber;
  35. int markPushBack = -1;
  36. /**
  37. * Constructs a newline number input stream that reads its input
  38. * from the specified input stream.
  39. *
  40. * @param in the underlying input stream.
  41. */
  42. public LineNumberInputStream(InputStream in) {
  43. super(in);
  44. }
  45. /**
  46. * Reads the next byte of data from this input stream. The value
  47. * byte is returned as an <code>int</code> in the range
  48. * <code>0</code> to <code>255</code>. If no byte is available
  49. * because the end of the stream has been reached, the value
  50. * <code>-1</code> is returned. This method blocks until input data
  51. * is available, the end of the stream is detected, or an exception
  52. * is thrown.
  53. * <p>
  54. * The <code>read</code> method of
  55. * <code>LineNumberInputStream</code> calls the <code>read</code>
  56. * method of the underlying input stream. It checks for carriage
  57. * returns and newline characters in the input, and modifies the
  58. * current line number as appropriate. A carriage-return character or
  59. * a carriage return followed by a newline character are both
  60. * converted into a single newline character.
  61. *
  62. * @return the next byte of data, or <code>-1</code> if the end of this
  63. * stream is reached.
  64. * @exception IOException if an I/O error occurs.
  65. * @see java.io.FilterInputStream#in
  66. * @see java.io.LineNumberInputStream#getLineNumber()
  67. */
  68. public int read() throws IOException {
  69. int c = pushBack;
  70. if (c != -1) {
  71. pushBack = -1;
  72. } else {
  73. c = in.read();
  74. }
  75. switch (c) {
  76. case '\r':
  77. pushBack = in.read();
  78. if (pushBack == '\n') {
  79. pushBack = -1;
  80. }
  81. case '\n':
  82. lineNumber++;
  83. return '\n';
  84. }
  85. return c;
  86. }
  87. /**
  88. * Reads up to <code>len</code> bytes of data from this input stream
  89. * into an array of bytes. This method blocks until some input is available.
  90. * <p>
  91. * The <code>read</code> method of
  92. * <code>LineNumberInputStream</code> repeatedly calls the
  93. * <code>read</code> method of zero arguments to fill in the byte array.
  94. *
  95. * @param b the buffer into which the data is read.
  96. * @param off the start offset of the data.
  97. * @param len the maximum number of bytes read.
  98. * @return the total number of bytes read into the buffer, or
  99. * <code>-1</code> if there is no more data because the end of
  100. * this stream has been reached.
  101. * @exception IOException if an I/O error occurs.
  102. * @see java.io.LineNumberInputStream#read()
  103. */
  104. public int read(byte b[], int off, int len) throws IOException {
  105. if (b == null) {
  106. throw new NullPointerException();
  107. } else if ((off < 0) || (off > b.length) || (len < 0) ||
  108. ((off + len) > b.length) || ((off + len) < 0)) {
  109. throw new IndexOutOfBoundsException();
  110. } else if (len == 0) {
  111. return 0;
  112. }
  113. int c = read();
  114. if (c == -1) {
  115. return -1;
  116. }
  117. b[off] = (byte)c;
  118. int i = 1;
  119. try {
  120. for (; i < len ; i++) {
  121. c = read();
  122. if (c == -1) {
  123. break;
  124. }
  125. if (b != null) {
  126. b[off + i] = (byte)c;
  127. }
  128. }
  129. } catch (IOException ee) {
  130. }
  131. return i;
  132. }
  133. /**
  134. * Skips over and discards <code>n</code> bytes of data from this
  135. * input stream. The <code>skip</code> method may, for a variety of
  136. * reasons, end up skipping over some smaller number of bytes,
  137. * possibly <code>0</code>. The actual number of bytes skipped is
  138. * returned. If <code>n</code> is negative, no bytes are skipped.
  139. * <p>
  140. * The <code>skip</code> method of <code>LineNumberInputStream</code> creates
  141. * a byte array and then repeatedly reads into it until
  142. * <code>n</code> bytes have been read or the end of the stream has
  143. * been reached.
  144. *
  145. * @param n the number of bytes to be skipped.
  146. * @return the actual number of bytes skipped.
  147. * @exception IOException if an I/O error occurs.
  148. * @see java.io.FilterInputStream#in
  149. */
  150. public long skip(long n) throws IOException {
  151. int chunk = 2048;
  152. long remaining = n;
  153. byte data[];
  154. int nr;
  155. if (n <= 0) {
  156. return 0;
  157. }
  158. data = new byte[chunk];
  159. while (remaining > 0) {
  160. nr = read(data, 0, (int) Math.min(chunk, remaining));
  161. if (nr < 0) {
  162. break;
  163. }
  164. remaining -= nr;
  165. }
  166. return n - remaining;
  167. }
  168. /**
  169. * Sets the line number to the specified argument.
  170. *
  171. * @param lineNumber the new line number.
  172. */
  173. public void setLineNumber(int lineNumber) {
  174. this.lineNumber = lineNumber;
  175. }
  176. /**
  177. * Returns the current line number.
  178. *
  179. * @return the current line number.
  180. */
  181. public int getLineNumber() {
  182. return lineNumber;
  183. }
  184. /**
  185. * Returns the number of bytes that can be read from this input
  186. * stream without blocking.
  187. * <p>
  188. * Note that if the underlying input stream is able to supply
  189. * <i>k</i> input characters without blocking, the
  190. * <code>LineNumberInputStream</code> can guarantee only to provide
  191. * <i>k</i>/2 characters without blocking, because the
  192. * <i>k</i> characters from the underlyhing input stream might
  193. * consist of <i>k</i>/2 pairs of <code>'\r'</code> and
  194. * <code>'\n'</code>, which are converted to just
  195. * <i>k</i>/2 <code>'\n'</code> characters.
  196. *
  197. * @return the number of bytes that can be read from this input stream
  198. * without blocking.
  199. * @exception IOException if an I/O error occurs.
  200. * @see java.io.FilterInputStream#in
  201. */
  202. public int available() throws IOException {
  203. return (pushBack == -1) ? super.available()/2 : super.available()/2 + 1;
  204. }
  205. /**
  206. * Marks the current position in this input stream. A subsequent
  207. * call to the <code>reset</code> method repositions this stream at
  208. * the last marked position so that subsequent reads re-read the same bytes.
  209. * <p>
  210. * The <code>mark</code> method of
  211. * <code>LineNumberInputStream</code> remembers the current line
  212. * number in a private variable, and then calls the <code>mark</code>
  213. * method of the underlying input stream.
  214. *
  215. * @param readlimit the maximum limit of bytes that can be read before
  216. * the mark position becomes invalid.
  217. * @see java.io.FilterInputStream#in
  218. * @see java.io.LineNumberInputStream#reset()
  219. */
  220. public void mark(int readlimit) {
  221. markLineNumber = lineNumber;
  222. markPushBack = pushBack;
  223. in.mark(readlimit);
  224. }
  225. /**
  226. * Repositions this stream to the position at the time the
  227. * <code>mark</code> method was last called on this input stream.
  228. * <p>
  229. * The <code>reset</code> method of
  230. * <code>LineNumberInputStream</code> resets the line number to be
  231. * the line number at the time the <code>mark</code> method was
  232. * called, and then calls the <code>reset</code> method of the
  233. * underlying input stream.
  234. * <p>
  235. * Stream marks are intended to be used in
  236. * situations where you need to read ahead a little to see what's in
  237. * the stream. Often this is most easily done by invoking some
  238. * general parser. If the stream is of the type handled by the
  239. * parser, it just chugs along happily. If the stream is not of
  240. * that type, the parser should toss an exception when it fails,
  241. * which, if it happens within readlimit bytes, allows the outer
  242. * code to reset the stream and try another parser.
  243. *
  244. * @exception IOException if an I/O error occurs.
  245. * @see java.io.FilterInputStream#in
  246. * @see java.io.LineNumberInputStream#mark(int)
  247. */
  248. public void reset() throws IOException {
  249. lineNumber = markLineNumber;
  250. pushBack = markPushBack;
  251. in.reset();
  252. }
  253. }