home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / java / io / PipedInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  3.2 KB  |  258 lines

  1. package java.io;
  2.  
  3. public class PipedInputStream extends InputStream {
  4.    boolean closedByWriter;
  5.    volatile boolean closedByReader;
  6.    boolean connected;
  7.    Thread readSide;
  8.    Thread writeSide;
  9.    private static final int DEFAULT_PIPE_SIZE = 1024;
  10.    protected static final int PIPE_SIZE = 1024;
  11.    protected byte[] buffer;
  12.    // $FF: renamed from: in int
  13.    protected int field_0;
  14.    protected int out;
  15.  
  16.    public PipedInputStream(PipedOutputStream var1) throws IOException {
  17.       this(var1, 1024);
  18.    }
  19.  
  20.    public PipedInputStream(PipedOutputStream var1, int var2) throws IOException {
  21.       this.closedByWriter = false;
  22.       this.closedByReader = false;
  23.       this.connected = false;
  24.       this.field_0 = -1;
  25.       this.out = 0;
  26.       this.initPipe(var2);
  27.       this.connect(var1);
  28.    }
  29.  
  30.    public PipedInputStream() {
  31.       this.closedByWriter = false;
  32.       this.closedByReader = false;
  33.       this.connected = false;
  34.       this.field_0 = -1;
  35.       this.out = 0;
  36.       this.initPipe(1024);
  37.    }
  38.  
  39.    public PipedInputStream(int var1) {
  40.       this.closedByWriter = false;
  41.       this.closedByReader = false;
  42.       this.connected = false;
  43.       this.field_0 = -1;
  44.       this.out = 0;
  45.       this.initPipe(var1);
  46.    }
  47.  
  48.    private void initPipe(int var1) {
  49.       if (var1 <= 0) {
  50.          throw new IllegalArgumentException("Pipe Size <= 0");
  51.       } else {
  52.          this.buffer = new byte[var1];
  53.       }
  54.    }
  55.  
  56.    public void connect(PipedOutputStream var1) throws IOException {
  57.       var1.connect(this);
  58.    }
  59.  
  60.    protected synchronized void receive(int var1) throws IOException {
  61.       this.checkStateForReceive();
  62.       this.writeSide = Thread.currentThread();
  63.       if (this.field_0 == this.out) {
  64.          this.awaitSpace();
  65.       }
  66.  
  67.       if (this.field_0 < 0) {
  68.          this.field_0 = 0;
  69.          this.out = 0;
  70.       }
  71.  
  72.       this.buffer[this.field_0++] = (byte)(var1 & 255);
  73.       if (this.field_0 >= this.buffer.length) {
  74.          this.field_0 = 0;
  75.       }
  76.  
  77.    }
  78.  
  79.    synchronized void receive(byte[] var1, int var2, int var3) throws IOException {
  80.       this.checkStateForReceive();
  81.       this.writeSide = Thread.currentThread();
  82.       int var4 = var3;
  83.  
  84.       while(var4 > 0) {
  85.          if (this.field_0 == this.out) {
  86.             this.awaitSpace();
  87.          }
  88.  
  89.          int var5 = 0;
  90.          if (this.out < this.field_0) {
  91.             var5 = this.buffer.length - this.field_0;
  92.          } else if (this.field_0 < this.out) {
  93.             if (this.field_0 == -1) {
  94.                this.field_0 = this.out = 0;
  95.                var5 = this.buffer.length - this.field_0;
  96.             } else {
  97.                var5 = this.out - this.field_0;
  98.             }
  99.          }
  100.  
  101.          if (var5 > var4) {
  102.             var5 = var4;
  103.          }
  104.  
  105.          assert var5 > 0;
  106.  
  107.          System.arraycopy(var1, var2, this.buffer, this.field_0, var5);
  108.          var4 -= var5;
  109.          var2 += var5;
  110.          this.field_0 += var5;
  111.          if (this.field_0 >= this.buffer.length) {
  112.             this.field_0 = 0;
  113.          }
  114.       }
  115.  
  116.    }
  117.  
  118.    private void checkStateForReceive() throws IOException {
  119.       if (!this.connected) {
  120.          throw new IOException("Pipe not connected");
  121.       } else if (!this.closedByWriter && !this.closedByReader) {
  122.          if (this.readSide != null && !this.readSide.isAlive()) {
  123.             throw new IOException("Read end dead");
  124.          }
  125.       } else {
  126.          throw new IOException("Pipe closed");
  127.       }
  128.    }
  129.  
  130.    private void awaitSpace() throws IOException {
  131.       while(this.field_0 == this.out) {
  132.          this.checkStateForReceive();
  133.          this.notifyAll();
  134.  
  135.          try {
  136.             this.wait(1000L);
  137.          } catch (InterruptedException var2) {
  138.             throw new InterruptedIOException();
  139.          }
  140.       }
  141.  
  142.    }
  143.  
  144.    synchronized void receivedLast() {
  145.       this.closedByWriter = true;
  146.       this.notifyAll();
  147.    }
  148.  
  149.    public synchronized int read() throws IOException {
  150.       if (!this.connected) {
  151.          throw new IOException("Pipe not connected");
  152.       } else if (this.closedByReader) {
  153.          throw new IOException("Pipe closed");
  154.       } else if (this.writeSide != null && !this.writeSide.isAlive() && !this.closedByWriter && this.field_0 < 0) {
  155.          throw new IOException("Write end dead");
  156.       } else {
  157.          this.readSide = Thread.currentThread();
  158.          int var1 = 2;
  159.  
  160.          while(this.field_0 < 0) {
  161.             if (this.closedByWriter) {
  162.                return -1;
  163.             }
  164.  
  165.             if (this.writeSide != null && !this.writeSide.isAlive()) {
  166.                --var1;
  167.                if (var1 < 0) {
  168.                   throw new IOException("Pipe broken");
  169.                }
  170.             }
  171.  
  172.             this.notifyAll();
  173.  
  174.             try {
  175.                this.wait(1000L);
  176.             } catch (InterruptedException var3) {
  177.                throw new InterruptedIOException();
  178.             }
  179.          }
  180.  
  181.          int var2 = this.buffer[this.out++] & 255;
  182.          if (this.out >= this.buffer.length) {
  183.             this.out = 0;
  184.          }
  185.  
  186.          if (this.field_0 == this.out) {
  187.             this.field_0 = -1;
  188.          }
  189.  
  190.          return var2;
  191.       }
  192.    }
  193.  
  194.    public synchronized int read(byte[] var1, int var2, int var3) throws IOException {
  195.       if (var1 == null) {
  196.          throw new NullPointerException();
  197.       } else if (var2 >= 0 && var3 >= 0 && var3 <= var1.length - var2) {
  198.          if (var3 == 0) {
  199.             return 0;
  200.          } else {
  201.             int var4 = this.read();
  202.             if (var4 < 0) {
  203.                return -1;
  204.             } else {
  205.                var1[var2] = (byte)var4;
  206.                int var5 = 1;
  207.  
  208.                while(this.field_0 >= 0 && var3 > 1) {
  209.                   int var6;
  210.                   if (this.field_0 > this.out) {
  211.                      var6 = Math.min(this.buffer.length - this.out, this.field_0 - this.out);
  212.                   } else {
  213.                      var6 = this.buffer.length - this.out;
  214.                   }
  215.  
  216.                   if (var6 > var3 - 1) {
  217.                      var6 = var3 - 1;
  218.                   }
  219.  
  220.                   System.arraycopy(this.buffer, this.out, var1, var2 + var5, var6);
  221.                   this.out += var6;
  222.                   var5 += var6;
  223.                   var3 -= var6;
  224.                   if (this.out >= this.buffer.length) {
  225.                      this.out = 0;
  226.                   }
  227.  
  228.                   if (this.field_0 == this.out) {
  229.                      this.field_0 = -1;
  230.                   }
  231.                }
  232.  
  233.                return var5;
  234.             }
  235.          }
  236.       } else {
  237.          throw new IndexOutOfBoundsException();
  238.       }
  239.    }
  240.  
  241.    public synchronized int available() throws IOException {
  242.       if (this.field_0 < 0) {
  243.          return 0;
  244.       } else if (this.field_0 == this.out) {
  245.          return this.buffer.length;
  246.       } else {
  247.          return this.field_0 > this.out ? this.field_0 - this.out : this.field_0 + this.buffer.length - this.out;
  248.       }
  249.    }
  250.  
  251.    public void close() throws IOException {
  252.       this.closedByReader = true;
  253.       synchronized(this) {
  254.          this.field_0 = -1;
  255.       }
  256.    }
  257. }
  258.