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

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