home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / io / LineNumberReader.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.8 KB  |  223 lines

  1. /*
  2.  * @(#)LineNumberReader.java    1.9 98/03/18
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * A buffered character-input stream that keeps track of line numbers.  A line
  20.  * is considered to be terminated by any one of a line feed ('\n'), a carriage
  21.  * return ('\r'), or a carriage return followed immediately by a linefeed.
  22.  *
  23.  * @version     1.9, 98/03/18
  24.  * @author    Mark Reinhold
  25.  * @since    JDK1.1
  26.  */
  27.  
  28. public class LineNumberReader extends BufferedReader {
  29.  
  30.     /** The current line number */
  31.     private int lineNumber = 0;
  32.  
  33.     /** The line number of the mark, if any */
  34.     private int markedLineNumber;
  35.  
  36.     /** If the next character is a line feed, skip it */
  37.     private boolean skipLF;
  38.  
  39.     /** The skipLF flag when the mark was set */
  40.     private boolean markedSkipLF;
  41.  
  42.     /**
  43.      * Create a new line-numbering reader, using the default input-buffer
  44.      * size.
  45.      */
  46.     public LineNumberReader(Reader in) {
  47.     super(in);
  48.     }
  49.  
  50.     /**
  51.      * Create a new line-numbering reader, reading characters into a buffer of
  52.      * the given size.
  53.      */
  54.     public LineNumberReader(Reader in, int sz) {
  55.     super(in, sz);
  56.     }
  57.  
  58.     /**
  59.      * Set the current line number.
  60.      */
  61.     public void setLineNumber(int lineNumber) {
  62.     this.lineNumber = lineNumber;
  63.     }
  64.  
  65.     /**
  66.      * Get the current line number.
  67.      */
  68.     public int getLineNumber() {
  69.     return lineNumber;
  70.     }
  71.  
  72.     /**
  73.      * Read a single character.  Line terminators are compressed into single
  74.      * newline ('\n') characters.
  75.      *
  76.      * @return     The character read, or -1 if the end of the stream has been
  77.      *             reached
  78.      *
  79.      * @exception  IOException  If an I/O error occurs
  80.      */
  81.     public int read() throws IOException {
  82.     synchronized (lock) {
  83.         int c = super.read();
  84.         if (skipLF) {
  85.         if (c == '\n')
  86.             c = super.read();
  87.         skipLF = false;
  88.         }
  89.         switch (c) {
  90.         case '\r':
  91.         skipLF = true;
  92.         case '\n':        /* Fall through */
  93.         lineNumber++;
  94.         return '\n';
  95.         }
  96.         return c;
  97.     }
  98.     }
  99.  
  100.     /**
  101.      * Read characters into a portion of an array.
  102.      *
  103.      * @param      cbuf  Destination buffer
  104.      * @param      off   Offset at which to start storing characters
  105.      * @param      len   Maximum number of characters to read
  106.      *
  107.      * @return     The number of bytes read, or -1 if the end of the stream has
  108.      *             already been reached
  109.      *
  110.      * @exception  IOException  If an I/O error occurs
  111.      */
  112.     public int read(char cbuf[], int off, int len) throws IOException {
  113.     synchronized (lock) {
  114.         int n = super.read(cbuf, off, len);
  115.  
  116.         for (int i = off; i < off + n; i++) {
  117.         int c = cbuf[i];
  118.         if (skipLF) {
  119.             skipLF = false;
  120.             if (c == '\n')
  121.             continue;
  122.         }
  123.         switch (c) {
  124.         case '\r':
  125.             skipLF = true;
  126.         case '\n':    /* Fall through */
  127.             lineNumber++;
  128.             break;
  129.         }
  130.         }
  131.  
  132.         return n;
  133.     }
  134.     }
  135.  
  136.     /**
  137.      * Read a line of text.  A line is considered to be terminated by any one
  138.      * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
  139.      * followed immediately by a linefeed.
  140.      *
  141.      * @return     A String containing the contents of the line, not including
  142.      *             any line-termination characters, or null if the end of the
  143.      *             stream has been reached
  144.      *
  145.      * @exception  IOException  If an I/O error occurs
  146.      */
  147.     public String readLine() throws IOException {
  148.     synchronized (lock) {
  149.         String l = super.readLine(skipLF);
  150.             skipLF = false;
  151.         if (l != null)
  152.         lineNumber++;
  153.         return l;
  154.     }
  155.     }
  156.  
  157.     /** Maximum skip-buffer size */
  158.     private static final int maxSkipBufferSize = 8192;
  159.  
  160.     /** Skip buffer, null until allocated */
  161.     private char skipBuffer[] = null;
  162.  
  163.     /**
  164.      * Skip characters.
  165.      *
  166.      * @param  n  The number of characters to skip
  167.      *
  168.      * @return    The number of characters actually skipped
  169.      *
  170.      * @exception  IOException  If an I/O error occurs
  171.      */
  172.     public long skip(long n) throws IOException {
  173.     int nn = (int) Math.min(n, maxSkipBufferSize);
  174.     synchronized (lock) {
  175.         if ((skipBuffer == null) || (skipBuffer.length < nn))
  176.         skipBuffer = new char[nn];
  177.         long r = n;
  178.         while (r > 0) {
  179.         int nc = read(skipBuffer, 0, (int) Math.min(r, nn));
  180.         if (nc == -1)
  181.             break;
  182.         r -= nc;
  183.         }
  184.         return n - r;
  185.     }
  186.     }
  187.  
  188.     /**
  189.      * Mark the present position in the stream.  Subsequent calls to reset()
  190.      * will attempt to reposition the stream to this point, and will also reset
  191.      * the line number appropriately.
  192.      *
  193.      * @param  readAheadLimit  Limit on the number of characters that may be
  194.      *                         read while still preserving the mark.  After
  195.      *                         reading this many characters, attempting to
  196.      *                         reset the stream may fail.
  197.      *
  198.      * @exception  IOException  If an I/O error occurs
  199.      */
  200.     public void mark(int readAheadLimit) throws IOException {
  201.     synchronized (lock) {
  202.         super.mark(readAheadLimit);
  203.         markedLineNumber = lineNumber;
  204.             markedSkipLF     = skipLF;
  205.     }
  206.     }
  207.  
  208.     /**
  209.      * Reset the stream to the most recent mark.
  210.      *
  211.      * @exception  IOException  If the stream has not been marked,
  212.      *                          or if the mark has been invalidated
  213.      */
  214.     public void reset() throws IOException {
  215.     synchronized (lock) {
  216.         super.reset();
  217.         lineNumber = markedLineNumber;
  218.             skipLF     = markedSkipLF;
  219.     }
  220.     }
  221.  
  222. }
  223.