home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &n…he Search for Life DVD 2 / DVD-ROM.iso / install / jre1_3 / lib / rt.jar / java / io / PipedInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  2.2 KB  |  180 lines

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