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

Print this page
rev 4802 : 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
Reviewed-by: alanb, dholmes, forax, sherman, smarks
Contributed-by: London Java Community and Michael Barker <mikeb01@gmail.com>


 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 {
 257                 return null;
 258             }
 259         }
 260         public Certificate[] getCertificates() {
 261             try {
 262                 maybeInstantiateVerifier();


 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;
 682                     return true;
 683                 }
 684                 return false;
 685             }
 686 
 687             public String nextElement() {
 688                 if (hasMoreElements()) {
 689                     String value = name;
 690                     name = null;
 691                     return value;
 692                 }
 693                 throw new NoSuchElementException();
 694             }
 695         };
 696     }


 709                 return cs;
 710             } else {
 711                 return jv.getCodeSource(url, name);
 712             }
 713         }
 714 
 715         return JarVerifier.getUnsignedCS(url);
 716     }
 717 
 718     void setEagerValidation(boolean eager) {
 719         try {
 720             maybeInstantiateVerifier();
 721         } catch (IOException e) {
 722             throw new RuntimeException(e);
 723         }
 724         if (jv != null) {
 725             jv.setEagerValidation(eager);
 726         }
 727     }
 728 
 729     List getManifestDigests() {
 730         ensureInitialization();
 731         if (jv != null) {
 732             return jv.getManifestDigests();
 733         }
 734         return new ArrayList();
 735     }
 736 }


 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<? 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 = 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 {
 257                 return null;
 258             }
 259         }
 260         public Certificate[] getCertificates() {
 261             try {
 262                 maybeInstantiateVerifier();


 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 = 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 = entries.nextElement();
 677                     value = e.getName();
 678                     if (e.isDirectory() || JarVerifier.isSigningRelated(value)) {
 679                         continue;
 680                     }
 681                     name = value;
 682                     return true;
 683                 }
 684                 return false;
 685             }
 686 
 687             public String nextElement() {
 688                 if (hasMoreElements()) {
 689                     String value = name;
 690                     name = null;
 691                     return value;
 692                 }
 693                 throw new NoSuchElementException();
 694             }
 695         };
 696     }


 709                 return cs;
 710             } else {
 711                 return jv.getCodeSource(url, name);
 712             }
 713         }
 714 
 715         return JarVerifier.getUnsignedCS(url);
 716     }
 717 
 718     void setEagerValidation(boolean eager) {
 719         try {
 720             maybeInstantiateVerifier();
 721         } catch (IOException e) {
 722             throw new RuntimeException(e);
 723         }
 724         if (jv != null) {
 725             jv.setEagerValidation(eager);
 726         }
 727     }
 728 
 729     List<Object> getManifestDigests() {
 730         ensureInitialization();
 731         if (jv != null) {
 732             return jv.getManifestDigests();
 733         }
 734         return new ArrayList<Object>();
 735     }
 736 }