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 / BufferedInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  2.7 KB  |  209 lines

  1. package java.io;
  2.  
  3. import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
  4.  
  5. public class BufferedInputStream extends FilterInputStream {
  6.    private static int defaultBufferSize = 8192;
  7.    protected volatile byte[] buf;
  8.    private static final AtomicReferenceFieldUpdater<BufferedInputStream, byte[]> bufUpdater = AtomicReferenceFieldUpdater.newUpdater(BufferedInputStream.class, byte[].class, "buf");
  9.    protected int count;
  10.    protected int pos;
  11.    protected int markpos;
  12.    protected int marklimit;
  13.  
  14.    private InputStream getInIfOpen() throws IOException {
  15.       InputStream var1 = this.in;
  16.       if (var1 == null) {
  17.          throw new IOException("Stream closed");
  18.       } else {
  19.          return var1;
  20.       }
  21.    }
  22.  
  23.    private byte[] getBufIfOpen() throws IOException {
  24.       byte[] var1 = this.buf;
  25.       if (var1 == null) {
  26.          throw new IOException("Stream closed");
  27.       } else {
  28.          return var1;
  29.       }
  30.    }
  31.  
  32.    public BufferedInputStream(InputStream var1) {
  33.       this(var1, defaultBufferSize);
  34.    }
  35.  
  36.    public BufferedInputStream(InputStream var1, int var2) {
  37.       super(var1);
  38.       this.markpos = -1;
  39.       if (var2 <= 0) {
  40.          throw new IllegalArgumentException("Buffer size <= 0");
  41.       } else {
  42.          this.buf = new byte[var2];
  43.       }
  44.    }
  45.  
  46.    private void fill() throws IOException {
  47.       byte[] var1 = this.getBufIfOpen();
  48.       if (this.markpos < 0) {
  49.          this.pos = 0;
  50.       } else if (this.pos >= var1.length) {
  51.          if (this.markpos > 0) {
  52.             int var2 = this.pos - this.markpos;
  53.             System.arraycopy(var1, this.markpos, var1, 0, var2);
  54.             this.pos = var2;
  55.             this.markpos = 0;
  56.          } else if (var1.length >= this.marklimit) {
  57.             this.markpos = -1;
  58.             this.pos = 0;
  59.          } else {
  60.             int var4 = this.pos * 2;
  61.             if (var4 > this.marklimit) {
  62.                var4 = this.marklimit;
  63.             }
  64.  
  65.             byte[] var3 = new byte[var4];
  66.             System.arraycopy(var1, 0, var3, 0, this.pos);
  67.             if (!bufUpdater.compareAndSet(this, var1, var3)) {
  68.                throw new IOException("Stream closed");
  69.             }
  70.  
  71.             var1 = var3;
  72.          }
  73.       }
  74.  
  75.       this.count = this.pos;
  76.       int var5 = this.getInIfOpen().read(var1, this.pos, var1.length - this.pos);
  77.       if (var5 > 0) {
  78.          this.count = var5 + this.pos;
  79.       }
  80.  
  81.    }
  82.  
  83.    public synchronized int read() throws IOException {
  84.       if (this.pos >= this.count) {
  85.          this.fill();
  86.          if (this.pos >= this.count) {
  87.             return -1;
  88.          }
  89.       }
  90.  
  91.       return this.getBufIfOpen()[this.pos++] & 255;
  92.    }
  93.  
  94.    private int read1(byte[] var1, int var2, int var3) throws IOException {
  95.       int var4 = this.count - this.pos;
  96.       if (var4 <= 0) {
  97.          if (var3 >= this.getBufIfOpen().length && this.markpos < 0) {
  98.             return this.getInIfOpen().read(var1, var2, var3);
  99.          }
  100.  
  101.          this.fill();
  102.          var4 = this.count - this.pos;
  103.          if (var4 <= 0) {
  104.             return -1;
  105.          }
  106.       }
  107.  
  108.       int var5 = var4 < var3 ? var4 : var3;
  109.       System.arraycopy(this.getBufIfOpen(), this.pos, var1, var2, var5);
  110.       this.pos += var5;
  111.       return var5;
  112.    }
  113.  
  114.    public synchronized int read(byte[] var1, int var2, int var3) throws IOException {
  115.       this.getBufIfOpen();
  116.       if ((var2 | var3 | var2 + var3 | var1.length - (var2 + var3)) < 0) {
  117.          throw new IndexOutOfBoundsException();
  118.       } else if (var3 == 0) {
  119.          return 0;
  120.       } else {
  121.          int var4 = 0;
  122.  
  123.          InputStream var6;
  124.          do {
  125.             int var5 = this.read1(var1, var2 + var4, var3 - var4);
  126.             if (var5 <= 0) {
  127.                return var4 == 0 ? var5 : var4;
  128.             }
  129.  
  130.             var4 += var5;
  131.             if (var4 >= var3) {
  132.                return var4;
  133.             }
  134.  
  135.             var6 = this.in;
  136.          } while(var6 == null || var6.available() > 0);
  137.  
  138.          return var4;
  139.       }
  140.    }
  141.  
  142.    public synchronized long skip(long var1) throws IOException {
  143.       this.getBufIfOpen();
  144.       if (var1 <= 0L) {
  145.          return 0L;
  146.       } else {
  147.          long var3 = (long)(this.count - this.pos);
  148.          if (var3 <= 0L) {
  149.             if (this.markpos < 0) {
  150.                return this.getInIfOpen().skip(var1);
  151.             }
  152.  
  153.             this.fill();
  154.             var3 = (long)(this.count - this.pos);
  155.             if (var3 <= 0L) {
  156.                return 0L;
  157.             }
  158.          }
  159.  
  160.          long var5 = var3 < var1 ? var3 : var1;
  161.          this.pos = (int)((long)this.pos + var5);
  162.          return var5;
  163.       }
  164.    }
  165.  
  166.    public synchronized int available() throws IOException {
  167.       return this.getInIfOpen().available() + (this.count - this.pos);
  168.    }
  169.  
  170.    public synchronized void mark(int var1) {
  171.       this.marklimit = var1;
  172.       this.markpos = this.pos;
  173.    }
  174.  
  175.    public synchronized void reset() throws IOException {
  176.       this.getBufIfOpen();
  177.       if (this.markpos < 0) {
  178.          throw new IOException("Resetting to invalid mark");
  179.       } else {
  180.          this.pos = this.markpos;
  181.       }
  182.    }
  183.  
  184.    public boolean markSupported() {
  185.       return true;
  186.    }
  187.  
  188.    public void close() throws IOException {
  189.       while(true) {
  190.          byte[] var1;
  191.          if ((var1 = this.buf) != null) {
  192.             if (!bufUpdater.compareAndSet(this, var1, (Object)null)) {
  193.                continue;
  194.             }
  195.  
  196.             InputStream var2 = this.in;
  197.             this.in = null;
  198.             if (var2 != null) {
  199.                var2.close();
  200.             }
  201.  
  202.             return;
  203.          }
  204.  
  205.          return;
  206.       }
  207.    }
  208. }
  209.