src/share/classes/java/util/jar/JarFile.java

Print this page
rev 4793 : 7117249: java.util warnings patches from LJC/Mike Barker


 166     }
 167 
 168     private Manifest getManifestFromReference() throws IOException {
 169         Manifest man = manRef != null ? manRef.get() : null;
 170 
 171         if (man == null) {
 172 
 173             JarEntry manEntry = getManEntry();
 174 
 175             // If found then load the manifest
 176             if (manEntry != null) {
 177                 if (verify) {
 178                     byte[] b = getBytes(manEntry);
 179                     man = new Manifest(new ByteArrayInputStream(b));
 180                     if (!jvInitialized) {
 181                         jv = new JarVerifier(b);
 182                     }
 183                 } else {
 184                     man = new Manifest(super.getInputStream(manEntry));
 185                 }
 186                 manRef = new SoftReference(man);
 187             }
 188         }
 189         return man;
 190     }
 191 
 192     private native String[] getMetaInfEntryNames();
 193 
 194     /**
 195      * Returns the <code>JarEntry</code> for the given entry name or
 196      * <code>null</code> if not found.
 197      *
 198      * @param name the jar file entry name
 199      * @return the <code>JarEntry</code> for the given entry name or
 200      *         <code>null</code> if not found.
 201      *
 202      * @throws IllegalStateException
 203      *         may be thrown if the jar file has been closed
 204      *
 205      * @see java.util.jar.JarEntry
 206      */


 216      * @return the <code>ZipEntry</code> for the given entry name or
 217      *         <code>null</code> if not found
 218      *
 219      * @throws IllegalStateException
 220      *         may be thrown if the jar file has been closed
 221      *
 222      * @see java.util.zip.ZipEntry
 223      */
 224     public ZipEntry getEntry(String name) {
 225         ZipEntry ze = super.getEntry(name);
 226         if (ze != null) {
 227             return new JarFileEntry(ze);
 228         }
 229         return null;
 230     }
 231 
 232     /**
 233      * Returns an enumeration of the zip file entries.
 234      */
 235     public Enumeration<JarEntry> entries() {
 236         final Enumeration enum_ = super.entries();
 237         return new Enumeration<JarEntry>() {
 238             public boolean hasMoreElements() {
 239                 return enum_.hasMoreElements();
 240             }
 241             public JarFileEntry nextElement() {
 242                 ZipEntry ze = (ZipEntry)enum_.nextElement();
 243                 return new JarFileEntry(ze);
 244             }
 245         };
 246     }
 247 
 248     private class JarFileEntry extends JarEntry {
 249         JarFileEntry(ZipEntry ze) {
 250             super(ze);
 251         }
 252         public Attributes getAttributes() throws IOException {
 253             Manifest man = JarFile.this.getManifest();
 254             if (man != null) {
 255                 return man.getAttributes(getName());
 256             } else {


 591 
 592                 public String nextElement() {
 593                     throw new NoSuchElementException();
 594                 }
 595             };
 596         }
 597     }
 598 
 599     /**
 600      * Returns an enumeration of the zip file entries
 601      * excluding internal JAR mechanism entries and including
 602      * signed entries missing from the ZIP directory.
 603      */
 604     Enumeration<JarEntry> entries2() {
 605         ensureInitialization();
 606         if (jv != null) {
 607             return jv.entries2(this, super.entries());
 608         }
 609 
 610         // screen out entries which are never signed
 611         final Enumeration enum_ = super.entries();
 612         return new Enumeration<JarEntry>() {
 613 
 614             ZipEntry entry;
 615 
 616             public boolean hasMoreElements() {
 617                 if (entry != null) {
 618                     return true;
 619                 }
 620                 while (enum_.hasMoreElements()) {
 621                     ZipEntry ze = (ZipEntry) enum_.nextElement();
 622                     if (JarVerifier.isSigningRelated(ze.getName())) {
 623                         continue;
 624                     }
 625                     entry = ze;
 626                     return true;
 627                 }
 628                 return false;
 629             }
 630 
 631             public JarFileEntry nextElement() {
 632                 if (hasMoreElements()) {
 633                     ZipEntry ze = entry;
 634                     entry = null;
 635                     return new JarFileEntry(ze);
 636                 }
 637                 throw new NoSuchElementException();
 638             }
 639         };
 640     }
 641 
 642     CodeSource[] getCodeSources(URL url) {
 643         ensureInitialization();
 644         if (jv != null) {
 645             return jv.getCodeSources(this, url);
 646         }
 647 
 648         /*
 649          * JAR file has no signed content. Is there a non-signing
 650          * code source?
 651          */
 652         Enumeration unsigned = unsignedEntryNames();
 653         if (unsigned.hasMoreElements()) {
 654             return new CodeSource[]{JarVerifier.getUnsignedCS(url)};
 655         } else {
 656             return null;
 657         }
 658     }
 659 
 660     private Enumeration<String> unsignedEntryNames() {
 661         final Enumeration entries = entries();
 662         return new Enumeration<String>() {
 663 
 664             String name;
 665 
 666             /*
 667              * Grab entries from ZIP directory but screen out
 668              * metadata.
 669              */
 670             public boolean hasMoreElements() {
 671                 if (name != null) {
 672                     return true;
 673                 }
 674                 while (entries.hasMoreElements()) {
 675                     String value;
 676                     ZipEntry e = (ZipEntry) entries.nextElement();
 677                     value = e.getName();
 678                     if (e.isDirectory() || JarVerifier.isSigningRelated(value)) {
 679                         continue;
 680                     }
 681                     name = value;




 166     }
 167 
 168     private Manifest getManifestFromReference() throws IOException {
 169         Manifest man = manRef != null ? manRef.get() : null;
 170 
 171         if (man == null) {
 172 
 173             JarEntry manEntry = getManEntry();
 174 
 175             // If found then load the manifest
 176             if (manEntry != null) {
 177                 if (verify) {
 178                     byte[] b = getBytes(manEntry);
 179                     man = new Manifest(new ByteArrayInputStream(b));
 180                     if (!jvInitialized) {
 181                         jv = new JarVerifier(b);
 182                     }
 183                 } else {
 184                     man = new Manifest(super.getInputStream(manEntry));
 185                 }
 186                 manRef = new SoftReference<Manifest>(man);
 187             }
 188         }
 189         return man;
 190     }
 191 
 192     private native String[] getMetaInfEntryNames();
 193 
 194     /**
 195      * Returns the <code>JarEntry</code> for the given entry name or
 196      * <code>null</code> if not found.
 197      *
 198      * @param name the jar file entry name
 199      * @return the <code>JarEntry</code> for the given entry name or
 200      *         <code>null</code> if not found.
 201      *
 202      * @throws IllegalStateException
 203      *         may be thrown if the jar file has been closed
 204      *
 205      * @see java.util.jar.JarEntry
 206      */


 216      * @return the <code>ZipEntry</code> for the given entry name or
 217      *         <code>null</code> if not found
 218      *
 219      * @throws IllegalStateException
 220      *         may be thrown if the jar file has been closed
 221      *
 222      * @see java.util.zip.ZipEntry
 223      */
 224     public ZipEntry getEntry(String name) {
 225         ZipEntry ze = super.getEntry(name);
 226         if (ze != null) {
 227             return new JarFileEntry(ze);
 228         }
 229         return null;
 230     }
 231 
 232     /**
 233      * Returns an enumeration of the zip file entries.
 234      */
 235     public Enumeration<JarEntry> entries() {
 236         final Enumeration<? extends ZipEntry> enum_ = super.entries();
 237         return new Enumeration<JarEntry>() {
 238             public boolean hasMoreElements() {
 239                 return enum_.hasMoreElements();
 240             }
 241             public JarFileEntry nextElement() {
 242                 ZipEntry ze = (ZipEntry)enum_.nextElement();
 243                 return new JarFileEntry(ze);
 244             }
 245         };
 246     }
 247 
 248     private class JarFileEntry extends JarEntry {
 249         JarFileEntry(ZipEntry ze) {
 250             super(ze);
 251         }
 252         public Attributes getAttributes() throws IOException {
 253             Manifest man = JarFile.this.getManifest();
 254             if (man != null) {
 255                 return man.getAttributes(getName());
 256             } else {


 591 
 592                 public String nextElement() {
 593                     throw new NoSuchElementException();
 594                 }
 595             };
 596         }
 597     }
 598 
 599     /**
 600      * Returns an enumeration of the zip file entries
 601      * excluding internal JAR mechanism entries and including
 602      * signed entries missing from the ZIP directory.
 603      */
 604     Enumeration<JarEntry> entries2() {
 605         ensureInitialization();
 606         if (jv != null) {
 607             return jv.entries2(this, super.entries());
 608         }
 609 
 610         // screen out entries which are never signed
 611         final Enumeration<? extends ZipEntry> enum_ = super.entries();
 612         return new Enumeration<JarEntry>() {
 613 
 614             ZipEntry entry;
 615 
 616             public boolean hasMoreElements() {
 617                 if (entry != null) {
 618                     return true;
 619                 }
 620                 while (enum_.hasMoreElements()) {
 621                     ZipEntry ze = (ZipEntry) enum_.nextElement();
 622                     if (JarVerifier.isSigningRelated(ze.getName())) {
 623                         continue;
 624                     }
 625                     entry = ze;
 626                     return true;
 627                 }
 628                 return false;
 629             }
 630 
 631             public JarFileEntry nextElement() {
 632                 if (hasMoreElements()) {
 633                     ZipEntry ze = entry;
 634                     entry = null;
 635                     return new JarFileEntry(ze);
 636                 }
 637                 throw new NoSuchElementException();
 638             }
 639         };
 640     }
 641 
 642     CodeSource[] getCodeSources(URL url) {
 643         ensureInitialization();
 644         if (jv != null) {
 645             return jv.getCodeSources(this, url);
 646         }
 647 
 648         /*
 649          * JAR file has no signed content. Is there a non-signing
 650          * code source?
 651          */
 652         Enumeration<String> unsigned = unsignedEntryNames();
 653         if (unsigned.hasMoreElements()) {
 654             return new CodeSource[]{JarVerifier.getUnsignedCS(url)};
 655         } else {
 656             return null;
 657         }
 658     }
 659 
 660     private Enumeration<String> unsignedEntryNames() {
 661         final Enumeration<JarEntry> entries = entries();
 662         return new Enumeration<String>() {
 663 
 664             String name;
 665 
 666             /*
 667              * Grab entries from ZIP directory but screen out
 668              * metadata.
 669              */
 670             public boolean hasMoreElements() {
 671                 if (name != null) {
 672                     return true;
 673                 }
 674                 while (entries.hasMoreElements()) {
 675                     String value;
 676                     ZipEntry e = (ZipEntry) entries.nextElement();
 677                     value = e.getName();
 678                     if (e.isDirectory() || JarVerifier.isSigningRelated(value)) {
 679                         continue;
 680                     }
 681                     name = value;