home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / rt.jar / java / io / PipedInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-04-23  |  2.6 KB  |  171 lines

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