home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / LineNumberInputStream.java < prev    next >
Text File  |  1996-05-03  |  5KB  |  175 lines

  1. /*
  2.  * @(#)LineNumberInputStream.java    1.8 95/12/19 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21.  
  22. /**
  23.  * An input stream that keeps track of line numbers.
  24.  *
  25.  * @version     1.8, 19 Dec 1995
  26.  * @author    Arthur van Hoff
  27.  */
  28. public
  29. class LineNumberInputStream extends FilterInputStream {
  30.     int pushBack = -1;
  31.     int lineNumber;
  32.     int markLineNumber;
  33.  
  34.     /**
  35.      * Constructs a new LineNumberInputStream initialized with
  36.      * the specified input stream.
  37.      * @param in the input stream
  38.      */
  39.     public LineNumberInputStream(InputStream in) {
  40.     super(in);
  41.     }
  42.  
  43.     /**
  44.      * Reads a byte of data. The method will block if no input is 
  45.      * available.
  46.      * @return     the byte read, or -1 if the end of the
  47.      *        stream is reached.
  48.      * @exception IOException If an I/O error has occurred.
  49.      */
  50.     public int read() throws IOException {
  51.     int c = pushBack;
  52.  
  53.     if (c != -1) {
  54.         pushBack = -1;
  55.     } else {
  56.         c = in.read();
  57.     }
  58.  
  59.     switch (c) {
  60.       case '\r':
  61.         pushBack = in.read();
  62.         if (pushBack == '\n') {
  63.         pushBack = -1;
  64.         }
  65.       case '\n':
  66.         lineNumber++;
  67.         return '\n';
  68.     }
  69.     return c;
  70.     }
  71.  
  72.     /**
  73.      * Reads into an array of bytes.  This method will
  74.      * blocks until some input is available.
  75.      * @param b    the buffer into which the data is read
  76.      * @param off the start offset of the data
  77.      * @param len the maximum number of bytes read
  78.      * @return  the actual number of bytes read, -1 is
  79.      *         returned when the end of the stream is reached.
  80.      * @exception IOException If an I/O error has occurred.
  81.      */
  82.     public int read(byte b[], int off, int len) throws IOException {
  83.     if (len <= 0) {
  84.         return 0;
  85.     }
  86.  
  87.     int c = read();
  88.     if (c == -1) {
  89.         return -1;
  90.     }
  91.     b[off] = (byte)c;
  92.  
  93.     int i = 1;
  94.     try {
  95.         for (; i < len ; i++) {
  96.         c = read();
  97.         if (c == -1) {
  98.             break;
  99.         }
  100.         if (b != null) {
  101.             b[off + i] = (byte)c;
  102.         }
  103.         }
  104.     } catch (IOException ee) {
  105.     }
  106.     return i;
  107.     }
  108.  
  109.     /**
  110.      * Sets the current line number.
  111.      * @param lineNumber the line number to be set
  112.      */
  113.     public void setLineNumber(int lineNumber) {
  114.     this.lineNumber = lineNumber;
  115.     }
  116.  
  117.     /**
  118.      * Returns the current line number.
  119.      */
  120.     public int getLineNumber() {
  121.     return lineNumber;
  122.     }
  123.  
  124.     /**
  125.      * Skips n bytes of input.
  126.      * @param n the number of bytes to be skipped
  127.      * @return    the actual number of bytes skipped.
  128.      * @exception IOException If an I/O error has occurred.
  129.      */
  130.     public long skip(long n) throws IOException {
  131.     return read(new byte[(int)n]);
  132.     }
  133.  
  134.     /**
  135.      * Returns the number of bytes that can be read 
  136.      * without blocking.
  137.      * @return the number of available bytes
  138.      */
  139.     public int available() throws IOException {
  140.     return (pushBack == -1) ? super.available() : super.available() + 1;
  141.     }
  142.  
  143.     /**
  144.      * Marks the current position in the input stream.  A subsequent
  145.      * call to reset() will reposition the stream at the last
  146.      * marked position so that subsequent reads will re-read
  147.      * the same bytes.  The stream promises to allow readlimit bytes
  148.      * to be read before the mark position gets invalidated.
  149.      * 
  150.      * @param readlimit the maximum limit of bytes allowed to be read
  151.      * before the mark position becomes invalid.
  152.      */
  153.     public void mark(int readlimit) {
  154.     markLineNumber = lineNumber;
  155.     in.mark(readlimit);
  156.     }
  157.  
  158.     /**
  159.      * Repositions the stream to the last marked position.  If the
  160.      * stream has not been marked, or if the mark has been invalidated,
  161.      * an IOException is thrown.  Stream marks are intended to be used in
  162.      * situations where you need to read ahead a little to see what's in
  163.      * the stream.  Often this is most easily done by invoking some
  164.      * general parser.  If the stream is of the type handled by the
  165.      * parser, it just chugs along happily.  If the stream is not of
  166.      * that type, the parser should toss an exception when it fails,
  167.      * which, if it happens within readlimit bytes, allows the outer
  168.      * code to reset the stream and try another parser.
  169.      */
  170.     public void reset() throws IOException {
  171.     lineNumber = markLineNumber;
  172.     in.reset();
  173.     }
  174. }
  175.