home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / LineNumberReader.java < prev    next >
Text File  |  1997-05-20  |  6KB  |  226 lines

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