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

  1. package java.net;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5.  
  6. class SocketInputStream extends FileInputStream {
  7.    private boolean eof;
  8.    private SocketImpl impl;
  9.    private byte[] temp = new byte[1];
  10.  
  11.    SocketInputStream(SocketImpl impl) throws IOException {
  12.       super(impl.getFileDescriptor());
  13.       this.impl = impl;
  14.    }
  15.  
  16.    private native int socketRead(byte[] var1, int var2, int var3) throws IOException;
  17.  
  18.    public int read(byte[] b) throws IOException {
  19.       return this.read(b, 0, b.length);
  20.    }
  21.  
  22.    public int read(byte[] b, int off, int length) throws IOException {
  23.       if (this.eof) {
  24.          return -1;
  25.       } else {
  26.          int n = this.socketRead(b, off, length);
  27.          if (n <= 0) {
  28.             this.eof = true;
  29.             return -1;
  30.          } else {
  31.             return n;
  32.          }
  33.       }
  34.    }
  35.  
  36.    public int read() throws IOException {
  37.       if (this.eof) {
  38.          return -1;
  39.       } else {
  40.          int n = this.read(this.temp, 0, 1);
  41.          return n <= 0 ? -1 : this.temp[0] & 255;
  42.       }
  43.    }
  44.  
  45.    public int skip(int numbytes) throws IOException {
  46.       int n = numbytes;
  47.  
  48.       for(byte[] data = new byte[numbytes]; n > 0; n -= this.read(data, 0, n)) {
  49.       }
  50.  
  51.       return numbytes;
  52.    }
  53.  
  54.    public int available() throws IOException {
  55.       return this.impl.available();
  56.    }
  57.  
  58.    public void close() throws IOException {
  59.       this.impl.close();
  60.    }
  61.  
  62.    protected void finalize() {
  63.    }
  64. }
  65.