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

Print this page
rev 7020 : 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
Contributed-by: akhil.arora@oracle.com, brian.goetz@oracle.com


  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.jar;
  27 
  28 import java.io.*;
  29 import java.lang.ref.SoftReference;
  30 import java.net.URL;
  31 import java.util.*;


  32 import java.util.zip.*;
  33 import java.security.CodeSigner;
  34 import java.security.cert.Certificate;
  35 import java.security.AccessController;
  36 import java.security.CodeSource;
  37 import sun.misc.IOUtils;
  38 import sun.security.action.GetPropertyAction;
  39 import sun.security.util.ManifestEntryVerifier;
  40 import sun.misc.SharedSecrets;
  41 
  42 /**
  43  * The <code>JarFile</code> class is used to read the contents of a jar file
  44  * from any file that can be opened with <code>java.io.RandomAccessFile</code>.
  45  * It extends the class <code>java.util.zip.ZipFile</code> with support
  46  * for reading an optional <code>Manifest</code> entry. The
  47  * <code>Manifest</code> can be used to specify meta-information about the
  48  * jar file and its entries.
  49  *
  50  * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
  51  * or method in this class will cause a {@link NullPointerException} to be


 218      * Returns the <code>ZipEntry</code> for the given entry name or
 219      * <code>null</code> if not found.
 220      *
 221      * @param name the jar file entry name
 222      * @return the <code>ZipEntry</code> for the given entry name or
 223      *         <code>null</code> if not found
 224      *
 225      * @throws IllegalStateException
 226      *         may be thrown if the jar file has been closed
 227      *
 228      * @see java.util.zip.ZipEntry
 229      */
 230     public ZipEntry getEntry(String name) {
 231         ZipEntry ze = super.getEntry(name);
 232         if (ze != null) {
 233             return new JarFileEntry(ze);
 234         }
 235         return null;
 236     }
 237 























 238     /**
 239      * Returns an enumeration of the zip file entries.
 240      */
 241     public Enumeration<JarEntry> entries() {
 242         final Enumeration<? extends ZipEntry> enum_ = super.entries();
 243         return new Enumeration<JarEntry>() {
 244             public boolean hasMoreElements() {
 245                 return enum_.hasMoreElements();
 246             }
 247             public JarFileEntry nextElement() {
 248                 ZipEntry ze = enum_.nextElement();
 249                 return new JarFileEntry(ze);
 250             }
 251         };





 252     }
 253 
 254     private class JarFileEntry extends JarEntry {
 255         JarFileEntry(ZipEntry ze) {
 256             super(ze);
 257         }
 258         public Attributes getAttributes() throws IOException {
 259             Manifest man = JarFile.this.getManifest();
 260             if (man != null) {
 261                 return man.getAttributes(getName());
 262             } else {
 263                 return null;
 264             }
 265         }
 266         public Certificate[] getCertificates() {
 267             try {
 268                 maybeInstantiateVerifier();
 269             } catch (IOException e) {
 270                 throw new RuntimeException(e);
 271             }




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.jar;
  27 
  28 import java.io.*;
  29 import java.lang.ref.SoftReference;
  30 import java.net.URL;
  31 import java.util.*;
  32 import java.util.stream.Stream;
  33 import java.util.stream.StreamSupport;
  34 import java.util.zip.*;
  35 import java.security.CodeSigner;
  36 import java.security.cert.Certificate;
  37 import java.security.AccessController;
  38 import java.security.CodeSource;
  39 import sun.misc.IOUtils;
  40 import sun.security.action.GetPropertyAction;
  41 import sun.security.util.ManifestEntryVerifier;
  42 import sun.misc.SharedSecrets;
  43 
  44 /**
  45  * The <code>JarFile</code> class is used to read the contents of a jar file
  46  * from any file that can be opened with <code>java.io.RandomAccessFile</code>.
  47  * It extends the class <code>java.util.zip.ZipFile</code> with support
  48  * for reading an optional <code>Manifest</code> entry. The
  49  * <code>Manifest</code> can be used to specify meta-information about the
  50  * jar file and its entries.
  51  *
  52  * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
  53  * or method in this class will cause a {@link NullPointerException} to be


 220      * Returns the <code>ZipEntry</code> for the given entry name or
 221      * <code>null</code> if not found.
 222      *
 223      * @param name the jar file entry name
 224      * @return the <code>ZipEntry</code> for the given entry name or
 225      *         <code>null</code> if not found
 226      *
 227      * @throws IllegalStateException
 228      *         may be thrown if the jar file has been closed
 229      *
 230      * @see java.util.zip.ZipEntry
 231      */
 232     public ZipEntry getEntry(String name) {
 233         ZipEntry ze = super.getEntry(name);
 234         if (ze != null) {
 235             return new JarFileEntry(ze);
 236         }
 237         return null;
 238     }
 239 
 240     protected class JarEntryIterator implements Enumeration<JarEntry>,
 241             Iterator<JarEntry>
 242     {
 243         final Iterator<ZipEntry> e = new ZipFile.ZipEntryIterator();
 244 
 245         public boolean hasMoreElements() {
 246             return e.hasNext();
 247         }
 248 
 249         public JarEntry nextElement() {
 250             return next();
 251         }
 252 
 253         public boolean hasNext() {
 254             return e.hasNext();
 255         }
 256 
 257         public JarEntry next() {
 258             ZipEntry ze = e.next();
 259             return new JarFileEntry(ze);
 260         }
 261     }
 262 
 263     /**
 264      * Returns an enumeration of the zip file entries.
 265      */
 266     public Enumeration<JarEntry> entries() {
 267         return new JarEntryIterator();







 268     }
 269 
 270     @Override
 271     public Stream<JarEntry> stream() {
 272         return StreamSupport.stream(Spliterators.spliterator(
 273                 new JarEntryIterator(), size(),
 274                 Spliterator.DISTINCT | Spliterator.IMMUTABLE | Spliterator.NONNULL));
 275     }
 276 
 277     private class JarFileEntry extends JarEntry {
 278         JarFileEntry(ZipEntry ze) {
 279             super(ze);
 280         }
 281         public Attributes getAttributes() throws IOException {
 282             Manifest man = JarFile.this.getManifest();
 283             if (man != null) {
 284                 return man.getAttributes(getName());
 285             } else {
 286                 return null;
 287             }
 288         }
 289         public Certificate[] getCertificates() {
 290             try {
 291                 maybeInstantiateVerifier();
 292             } catch (IOException e) {
 293                 throw new RuntimeException(e);
 294             }