home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VPage / Java.bin / CLASSES.ZIP / sun / rmi / transport / tcp / MultiplexInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-07-08  |  2.6 KB  |  151 lines

  1. package sun.rmi.transport.tcp;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6.  
  7. final class MultiplexInputStream extends InputStream {
  8.    private ConnectionMultiplexer manager;
  9.    private MultiplexConnectionInfo info;
  10.    private byte[] buffer;
  11.    private int present;
  12.    private int pos;
  13.    private int requested;
  14.    private boolean disconnected = false;
  15.    private Object lock = new Object();
  16.    private int waterMark;
  17.    private byte[] temp = new byte[1];
  18.  
  19.    MultiplexInputStream(ConnectionMultiplexer var1, MultiplexConnectionInfo var2, int var3) {
  20.       this.manager = var1;
  21.       this.info = var2;
  22.       this.buffer = new byte[var3];
  23.       this.waterMark = var3 / 2;
  24.    }
  25.  
  26.    public synchronized int read() throws IOException {
  27.       int var1 = this.read(this.temp, 0, 1);
  28.       return var1 != 1 ? -1 : this.temp[0] & 255;
  29.    }
  30.  
  31.    public synchronized int read(byte[] var1, int var2, int var3) throws IOException {
  32.       if (var3 <= 0) {
  33.          return 0;
  34.       } else {
  35.          Object var5 = this.lock;
  36.          synchronized(var5){}
  37.  
  38.          int var4;
  39.          try {
  40.             if (this.pos >= this.present) {
  41.                this.pos = this.present = 0;
  42.             } else if (this.pos >= this.waterMark) {
  43.                System.arraycopy(this.buffer, this.pos, this.buffer, 0, this.waterMark);
  44.                this.present -= this.waterMark;
  45.                this.pos -= this.waterMark;
  46.             }
  47.  
  48.             int var7 = this.buffer.length - this.present;
  49.             var4 = Math.max(var7 - this.requested, 0);
  50.          } catch (Throwable var16) {
  51.             throw var16;
  52.          }
  53.  
  54.          if (var4 > 0) {
  55.             this.manager.sendRequest(this.info, var4);
  56.          }
  57.  
  58.          Object var6 = this.lock;
  59.          synchronized(var6){}
  60.  
  61.          try {
  62.             this.requested += var4;
  63.  
  64.             while(this.pos >= this.present && !this.disconnected) {
  65.                try {
  66.                   this.lock.wait();
  67.                } catch (InterruptedException var15) {
  68.                }
  69.             }
  70.  
  71.             if (this.disconnected && this.pos >= this.present) {
  72.                byte var20 = -1;
  73.                return var20;
  74.             }
  75.  
  76.             int var8 = this.present - this.pos;
  77.             if (var3 >= var8) {
  78.                System.arraycopy(this.buffer, this.pos, var1, var2, var8);
  79.                this.pos = this.present = 0;
  80.                int var19 = var8;
  81.                return var19;
  82.             }
  83.  
  84.             System.arraycopy(this.buffer, this.pos, var1, var2, var3);
  85.             this.pos += var3;
  86.             var18 = var3;
  87.          } catch (Throwable var17) {
  88.             throw var17;
  89.          }
  90.  
  91.          return var18;
  92.       }
  93.    }
  94.  
  95.    public int available() throws IOException {
  96.       Object var2 = this.lock;
  97.       synchronized(var2){}
  98.  
  99.       int var1;
  100.       try {
  101.          var1 = this.present - this.pos;
  102.       } catch (Throwable var5) {
  103.          throw var5;
  104.       }
  105.  
  106.       return var1;
  107.    }
  108.  
  109.    public void close() throws IOException {
  110.       this.manager.sendClose(this.info);
  111.    }
  112.  
  113.    void receive(int var1, DataInputStream var2) throws IOException {
  114.       Object var3 = this.lock;
  115.       synchronized(var3){}
  116.  
  117.       try {
  118.          if (this.pos > 0 && this.buffer.length - this.present < var1) {
  119.             System.arraycopy(this.buffer, this.pos, this.buffer, 0, this.present - this.pos);
  120.             this.present -= this.pos;
  121.             this.pos = 0;
  122.          }
  123.  
  124.          if (this.buffer.length - this.present < var1) {
  125.             throw new IOException("Receive buffer overflow");
  126.          }
  127.  
  128.          var2.readFully(this.buffer, this.present, var1);
  129.          this.present += var1;
  130.          this.requested -= var1;
  131.          this.lock.notifyAll();
  132.       } catch (Throwable var5) {
  133.          throw var5;
  134.       }
  135.  
  136.    }
  137.  
  138.    void disconnect() {
  139.       Object var1 = this.lock;
  140.       synchronized(var1){}
  141.  
  142.       try {
  143.          this.disconnected = true;
  144.          this.lock.notifyAll();
  145.       } catch (Throwable var3) {
  146.          throw var3;
  147.       }
  148.  
  149.    }
  150. }
  151.