< prev index next >

src/java.base/share/classes/java/util/zip/ZipFile.java

Print this page




 674         if (closeRequested) {
 675             throw new IllegalStateException("zip file closed");
 676         }
 677 
 678         if (jzfile == 0) {
 679             throw new IllegalStateException("The object is not initialized.");
 680         }
 681     }
 682 
 683     private void ensureOpenOrZipException() throws IOException {
 684         if (closeRequested) {
 685             throw new ZipException("ZipFile closed");
 686         }
 687     }
 688 
 689     /*
 690      * Inner class implementing the input stream used to read a
 691      * (possibly compressed) zip file entry.
 692      */
 693    private class ZipFileInputStream extends InputStream {
 694         private volatile boolean closeRequested = false;
 695         protected long jzentry; // address of jzentry data
 696         private   long pos;     // current position within entry data
 697         protected long rem;     // number of remaining bytes within entry
 698         protected long size;    // uncompressed size of this entry
 699 
 700         ZipFileInputStream(long jzentry) {
 701             pos = 0;
 702             rem = getEntryCSize(jzentry);
 703             size = getEntrySize(jzentry);
 704             this.jzentry = jzentry;
 705         }
 706 
 707         public int read(byte b[], int off, int len) throws IOException {
 708             synchronized (ZipFile.this) {
 709                 long rem = this.rem;
 710                 long pos = this.pos;
 711                 if (rem == 0) {
 712                     return -1;
 713                 }
 714                 if (len <= 0) {
 715                     return 0;
 716                 }
 717                 if (len > rem) {
 718                     len = (int) rem;
 719                 }
 720 

 721                 ensureOpenOrZipException();
 722                 len = ZipFile.read(ZipFile.this.jzfile, jzentry, pos, b,
 723                                    off, len);
 724                 if (len > 0) {
 725                     this.pos = (pos + len);
 726                     this.rem = (rem - len);
 727                 }
 728             }
 729             if (rem == 0) {
 730                 close();
 731             }
 732             return len;
 733         }
 734 
 735         public int read() throws IOException {
 736             byte[] b = new byte[1];
 737             if (read(b, 0, 1) == 1) {
 738                 return b[0] & 0xff;
 739             } else {
 740                 return -1;


 744         public long skip(long n) {
 745             if (n > rem)
 746                 n = rem;
 747             pos += n;
 748             rem -= n;
 749             if (rem == 0) {
 750                 close();
 751             }
 752             return n;
 753         }
 754 
 755         public int available() {
 756             return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
 757         }
 758 
 759         public long size() {
 760             return size;
 761         }
 762 
 763         public void close() {
 764             if (closeRequested)
 765                 return;
 766             closeRequested = true;
 767 
 768             rem = 0;
 769             synchronized (ZipFile.this) {
 770                 if (jzentry != 0 && ZipFile.this.jzfile != 0) {
 771                     freeEntry(ZipFile.this.jzfile, jzentry);
 772                     jzentry = 0;
 773                 }
 774             }
 775             synchronized (streams) {
 776                 streams.remove(this);
 777             }
 778         }
 779 
 780         protected void finalize() {
 781             close();
 782         }
 783     }
 784 
 785     static {
 786         SharedSecrets.setJavaUtilZipFileAccess(




 674         if (closeRequested) {
 675             throw new IllegalStateException("zip file closed");
 676         }
 677 
 678         if (jzfile == 0) {
 679             throw new IllegalStateException("The object is not initialized.");
 680         }
 681     }
 682 
 683     private void ensureOpenOrZipException() throws IOException {
 684         if (closeRequested) {
 685             throw new ZipException("ZipFile closed");
 686         }
 687     }
 688 
 689     /*
 690      * Inner class implementing the input stream used to read a
 691      * (possibly compressed) zip file entry.
 692      */
 693    private class ZipFileInputStream extends InputStream {
 694         private volatile boolean zfisCloseRequested = false;
 695         protected long jzentry; // address of jzentry data
 696         private   long pos;     // current position within entry data
 697         protected long rem;     // number of remaining bytes within entry
 698         protected long size;    // uncompressed size of this entry
 699 
 700         ZipFileInputStream(long jzentry) {
 701             pos = 0;
 702             rem = getEntryCSize(jzentry);
 703             size = getEntrySize(jzentry);
 704             this.jzentry = jzentry;
 705         }
 706 
 707         public int read(byte b[], int off, int len) throws IOException {
 708             synchronized (ZipFile.this) {
 709                 long rem = this.rem;
 710                 long pos = this.pos;
 711                 if (rem == 0) {
 712                     return -1;
 713                 }
 714                 if (len <= 0) {
 715                     return 0;
 716                 }
 717                 if (len > rem) {
 718                     len = (int) rem;
 719                 }
 720 
 721                 // Check if ZipFile open
 722                 ensureOpenOrZipException();
 723                 len = ZipFile.read(ZipFile.this.jzfile, jzentry, pos, b,
 724                                    off, len);
 725                 if (len > 0) {
 726                     this.pos = (pos + len);
 727                     this.rem = (rem - len);
 728                 }
 729             }
 730             if (rem == 0) {
 731                 close();
 732             }
 733             return len;
 734         }
 735 
 736         public int read() throws IOException {
 737             byte[] b = new byte[1];
 738             if (read(b, 0, 1) == 1) {
 739                 return b[0] & 0xff;
 740             } else {
 741                 return -1;


 745         public long skip(long n) {
 746             if (n > rem)
 747                 n = rem;
 748             pos += n;
 749             rem -= n;
 750             if (rem == 0) {
 751                 close();
 752             }
 753             return n;
 754         }
 755 
 756         public int available() {
 757             return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
 758         }
 759 
 760         public long size() {
 761             return size;
 762         }
 763 
 764         public void close() {
 765             if (zfisCloseRequested)
 766                 return;
 767             zfisCloseRequested = true;
 768 
 769             rem = 0;
 770             synchronized (ZipFile.this) {
 771                 if (jzentry != 0 && ZipFile.this.jzfile != 0) {
 772                     freeEntry(ZipFile.this.jzfile, jzentry);
 773                     jzentry = 0;
 774                 }
 775             }
 776             synchronized (streams) {
 777                 streams.remove(this);
 778             }
 779         }
 780 
 781         protected void finalize() {
 782             close();
 783         }
 784     }
 785 
 786     static {
 787         SharedSecrets.setJavaUtilZipFileAccess(


< prev index next >