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

  1. package java.io;
  2.  
  3. public class PipedInputStream extends InputStream {
  4.    boolean closed = true;
  5.    Thread readSide;
  6.    Thread writeSide;
  7.    private byte[] buffer = new byte[1024];
  8.    // $FF: renamed from: in int
  9.    int field_0 = -1;
  10.    int out;
  11.  
  12.    public PipedInputStream(PipedOutputStream src) throws IOException {
  13.       this.connect(src);
  14.    }
  15.  
  16.    public PipedInputStream() {
  17.    }
  18.  
  19.    public void connect(PipedOutputStream src) throws IOException {
  20.       src.connect(this);
  21.    }
  22.  
  23.    synchronized void receive(int b) throws IOException {
  24.       this.writeSide = Thread.currentThread();
  25.  
  26.       while(this.field_0 == this.out) {
  27.          if (this.readSide != null && !this.readSide.isAlive()) {
  28.             throw new IOException("Pipe broken");
  29.          }
  30.  
  31.          this.notifyAll();
  32.  
  33.          try {
  34.             this.wait(1000L);
  35.          } catch (InterruptedException var2) {
  36.             throw new InterruptedIOException();
  37.          }
  38.       }
  39.  
  40.       if (this.field_0 < 0) {
  41.          this.field_0 = 0;
  42.          this.out = 0;
  43.       }
  44.  
  45.       this.buffer[this.field_0++] = (byte)(b & 255);
  46.       if (this.field_0 >= this.buffer.length) {
  47.          this.field_0 = 0;
  48.       }
  49.  
  50.    }
  51.  
  52.    synchronized void receive(byte[] b, int off, int len) throws IOException {
  53.       while(true) {
  54.          --len;
  55.          if (len < 0) {
  56.             return;
  57.          }
  58.  
  59.          this.receive(b[off++]);
  60.       }
  61.    }
  62.  
  63.    synchronized void receivedLast() {
  64.       this.closed = true;
  65.       this.notifyAll();
  66.    }
  67.  
  68.    public synchronized int read() throws IOException {
  69.       int trials = 2;
  70.  
  71.       while(this.field_0 < 0) {
  72.          this.readSide = Thread.currentThread();
  73.          if (this.writeSide != null && !this.writeSide.isAlive()) {
  74.             --trials;
  75.             if (trials < 0) {
  76.                throw new IOException("Pipe broken");
  77.             }
  78.          }
  79.  
  80.          if (this.closed) {
  81.             return -1;
  82.          }
  83.  
  84.          this.notifyAll();
  85.  
  86.          try {
  87.             this.wait(1000L);
  88.          } catch (InterruptedException var3) {
  89.             throw new InterruptedIOException();
  90.          }
  91.       }
  92.  
  93.       int ret = this.buffer[this.out++] & 255;
  94.       if (this.out >= this.buffer.length) {
  95.          this.out = 0;
  96.       }
  97.  
  98.       if (this.field_0 == this.out) {
  99.          this.field_0 = -1;
  100.       }
  101.  
  102.       return ret;
  103.    }
  104.  
  105.    public synchronized int read(byte[] b, int off, int len) throws IOException {
  106.       if (len <= 0) {
  107.          return 0;
  108.       } else {
  109.          int c = this.read();
  110.          if (c < 0) {
  111.             return -1;
  112.          } else {
  113.             b[off] = (byte)c;
  114.             int rlen = 1;
  115.  
  116.             while(this.field_0 >= 0) {
  117.                --len;
  118.                if (len <= 0) {
  119.                   break;
  120.                }
  121.  
  122.                b[off + rlen] = this.buffer[this.out++];
  123.                ++rlen;
  124.                if (this.out >= this.buffer.length) {
  125.                   this.out = 0;
  126.                }
  127.  
  128.                if (this.field_0 == this.out) {
  129.                   this.field_0 = -1;
  130.                }
  131.             }
  132.  
  133.             return rlen;
  134.          }
  135.       }
  136.    }
  137.  
  138.    public void close() throws IOException {
  139.       this.field_0 = -1;
  140.       this.closed = true;
  141.    }
  142. }
  143.