home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / io / LineNumberInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  1.7 KB  |  90 lines

  1. package java.io;
  2.  
  3. public class LineNumberInputStream extends FilterInputStream {
  4.    int pushBack = -1;
  5.    int lineNumber;
  6.    int markLineNumber;
  7.  
  8.    public LineNumberInputStream(InputStream in) {
  9.       super(in);
  10.    }
  11.  
  12.    public int read() throws IOException {
  13.       int c = this.pushBack;
  14.       if (c != -1) {
  15.          this.pushBack = -1;
  16.       } else {
  17.          c = super.in.read();
  18.       }
  19.  
  20.       switch (c) {
  21.          case 13:
  22.             this.pushBack = super.in.read();
  23.             if (this.pushBack == 10) {
  24.                this.pushBack = -1;
  25.             }
  26.          case 10:
  27.             ++this.lineNumber;
  28.             return 10;
  29.          default:
  30.             return c;
  31.       }
  32.    }
  33.  
  34.    public int read(byte[] b, int off, int len) throws IOException {
  35.       if (len <= 0) {
  36.          return 0;
  37.       } else {
  38.          int c = this.read();
  39.          if (c == -1) {
  40.             return -1;
  41.          } else {
  42.             b[off] = (byte)c;
  43.             int i = 1;
  44.  
  45.             try {
  46.                for(; i < len; ++i) {
  47.                   c = this.read();
  48.                   if (c == -1) {
  49.                      break;
  50.                   }
  51.  
  52.                   if (b != null) {
  53.                      b[off + i] = (byte)c;
  54.                   }
  55.                }
  56.             } catch (IOException var6) {
  57.             }
  58.  
  59.             return i;
  60.          }
  61.       }
  62.    }
  63.  
  64.    public void setLineNumber(int lineNumber) {
  65.       this.lineNumber = lineNumber;
  66.    }
  67.  
  68.    public int getLineNumber() {
  69.       return this.lineNumber;
  70.    }
  71.  
  72.    public long skip(long n) throws IOException {
  73.       return (long)((FilterInputStream)this).read(new byte[(int)n]);
  74.    }
  75.  
  76.    public int available() throws IOException {
  77.       return this.pushBack == -1 ? super.available() : super.available() + 1;
  78.    }
  79.  
  80.    public void mark(int readlimit) {
  81.       this.markLineNumber = this.lineNumber;
  82.       super.in.mark(readlimit);
  83.    }
  84.  
  85.    public void reset() throws IOException {
  86.       this.lineNumber = this.markLineNumber;
  87.       super.in.reset();
  88.    }
  89. }
  90.