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 / PipedReader.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  2.2 KB  |  186 lines

  1. package java.io;
  2.  
  3. public class PipedReader extends Reader {
  4.    boolean closedByWriter = false;
  5.    boolean closedByReader = false;
  6.    boolean connected = false;
  7.    Thread readSide;
  8.    Thread writeSide;
  9.    static final int PIPE_SIZE = 1024;
  10.    char[] buffer = new char[1024];
  11.    // $FF: renamed from: in int
  12.    int field_0 = -1;
  13.    int out = 0;
  14.  
  15.    public PipedReader(PipedWriter var1) throws IOException {
  16.       this.connect(var1);
  17.    }
  18.  
  19.    public PipedReader() {
  20.    }
  21.  
  22.    public void connect(PipedWriter var1) throws IOException {
  23.       var1.connect(this);
  24.    }
  25.  
  26.    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++] = (char)var1;
  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(char[] 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.          char var2 = this.buffer[this.out++];
  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(char[] var1, int var2, int var3) throws IOException {
  127.       if (!this.connected) {
  128.          throw new IOException("Pipe not connected");
  129.       } else if (this.closedByReader) {
  130.          throw new IOException("Pipe closed");
  131.       } else if (this.writeSide != null && !this.writeSide.isAlive() && !this.closedByWriter && this.field_0 < 0) {
  132.          throw new IOException("Write end dead");
  133.       } else if (var2 >= 0 && var2 <= var1.length && var3 >= 0 && var2 + var3 <= var1.length && var2 + var3 >= 0) {
  134.          if (var3 == 0) {
  135.             return 0;
  136.          } else {
  137.             int var4 = this.read();
  138.             if (var4 < 0) {
  139.                return -1;
  140.             } else {
  141.                var1[var2] = (char)var4;
  142.                int var5 = 1;
  143.  
  144.                while(this.field_0 >= 0) {
  145.                   --var3;
  146.                   if (var3 <= 0) {
  147.                      break;
  148.                   }
  149.  
  150.                   var1[var2 + var5] = this.buffer[this.out++];
  151.                   ++var5;
  152.                   if (this.out >= this.buffer.length) {
  153.                      this.out = 0;
  154.                   }
  155.  
  156.                   if (this.field_0 == this.out) {
  157.                      this.field_0 = -1;
  158.                   }
  159.                }
  160.  
  161.                return var5;
  162.             }
  163.          }
  164.       } else {
  165.          throw new IndexOutOfBoundsException();
  166.       }
  167.    }
  168.  
  169.    public synchronized boolean ready() throws IOException {
  170.       if (!this.connected) {
  171.          throw new IOException("Pipe not connected");
  172.       } else if (this.closedByReader) {
  173.          throw new IOException("Pipe closed");
  174.       } else if (this.writeSide != null && !this.writeSide.isAlive() && !this.closedByWriter && this.field_0 < 0) {
  175.          throw new IOException("Write end dead");
  176.       } else {
  177.          return this.field_0 >= 0;
  178.       }
  179.    }
  180.  
  181.    public void close() throws IOException {
  182.       this.field_0 = -1;
  183.       this.closedByReader = true;
  184.    }
  185. }
  186.