jdk/src/share/classes/java/util/zip/ZipFile.java
changeset 17421 f3fbcfe6e2cf
parent 9676 5663e62f8d7e
child 17910 82d10099a8a6
child 18251 3743160a4cb8
equal deleted inserted replaced
17420:6163a8236046 17421:f3fbcfe6e2cf
    34 import java.nio.charset.StandardCharsets;
    34 import java.nio.charset.StandardCharsets;
    35 import java.util.ArrayDeque;
    35 import java.util.ArrayDeque;
    36 import java.util.Deque;
    36 import java.util.Deque;
    37 import java.util.Enumeration;
    37 import java.util.Enumeration;
    38 import java.util.HashMap;
    38 import java.util.HashMap;
       
    39 import java.util.Iterator;
    39 import java.util.Map;
    40 import java.util.Map;
    40 import java.util.NoSuchElementException;
    41 import java.util.NoSuchElementException;
       
    42 import java.util.Spliterator;
       
    43 import java.util.Spliterators;
    41 import java.util.WeakHashMap;
    44 import java.util.WeakHashMap;
    42 import java.security.AccessController;
    45 import java.util.stream.Stream;
    43 import sun.security.action.GetPropertyAction;
    46 import java.util.stream.StreamSupport;
       
    47 
    44 import static java.util.zip.ZipConstants64.*;
    48 import static java.util.zip.ZipConstants64.*;
    45 
    49 
    46 /**
    50 /**
    47  * This class is used to read entries from a zip file.
    51  * This class is used to read entries from a zip file.
    48  *
    52  *
   469      */
   473      */
   470     public String getName() {
   474     public String getName() {
   471         return name;
   475         return name;
   472     }
   476     }
   473 
   477 
       
   478     private class ZipEntryIterator implements Enumeration<ZipEntry>, Iterator<ZipEntry> {
       
   479         private int i = 0;
       
   480 
       
   481         public ZipEntryIterator() {
       
   482             ensureOpen();
       
   483         }
       
   484 
       
   485         public boolean hasMoreElements() {
       
   486             return hasNext();
       
   487         }
       
   488 
       
   489         public boolean hasNext() {
       
   490             synchronized (ZipFile.this) {
       
   491                 ensureOpen();
       
   492                 return i < total;
       
   493             }
       
   494         }
       
   495 
       
   496         public ZipEntry nextElement() {
       
   497             return next();
       
   498         }
       
   499 
       
   500         public ZipEntry next() {
       
   501             synchronized (ZipFile.this) {
       
   502                 ensureOpen();
       
   503                 if (i >= total) {
       
   504                     throw new NoSuchElementException();
       
   505                 }
       
   506                 long jzentry = getNextEntry(jzfile, i++);
       
   507                 if (jzentry == 0) {
       
   508                     String message;
       
   509                     if (closeRequested) {
       
   510                         message = "ZipFile concurrently closed";
       
   511                     } else {
       
   512                         message = getZipMessage(ZipFile.this.jzfile);
       
   513                     }
       
   514                     throw new ZipError("jzentry == 0" +
       
   515                                        ",\n jzfile = " + ZipFile.this.jzfile +
       
   516                                        ",\n total = " + ZipFile.this.total +
       
   517                                        ",\n name = " + ZipFile.this.name +
       
   518                                        ",\n i = " + i +
       
   519                                        ",\n message = " + message
       
   520                         );
       
   521                 }
       
   522                 ZipEntry ze = getZipEntry(null, jzentry);
       
   523                 freeEntry(jzfile, jzentry);
       
   524                 return ze;
       
   525             }
       
   526         }
       
   527     }
       
   528 
   474     /**
   529     /**
   475      * Returns an enumeration of the ZIP file entries.
   530      * Returns an enumeration of the ZIP file entries.
   476      * @return an enumeration of the ZIP file entries
   531      * @return an enumeration of the ZIP file entries
   477      * @throws IllegalStateException if the zip file has been closed
   532      * @throws IllegalStateException if the zip file has been closed
   478      */
   533      */
   479     public Enumeration<? extends ZipEntry> entries() {
   534     public Enumeration<? extends ZipEntry> entries() {
   480         ensureOpen();
   535         return new ZipEntryIterator();
   481         return new Enumeration<ZipEntry>() {
   536     }
   482                 private int i = 0;
   537 
   483                 public boolean hasMoreElements() {
   538     /**
   484                     synchronized (ZipFile.this) {
   539      * Return an ordered {@code Stream} over the ZIP file entries.
   485                         ensureOpen();
   540      * Entries appear in the {@code Stream} in the order they appear in
   486                         return i < total;
   541      * the central directory of the ZIP file.
   487                     }
   542      *
   488                 }
   543      * @return an ordered {@code Stream} of entries in this ZIP file
   489                 public ZipEntry nextElement() throws NoSuchElementException {
   544      * @throws IllegalStateException if the zip file has been closed
   490                     synchronized (ZipFile.this) {
   545      * @since 1.8
   491                         ensureOpen();
   546      */
   492                         if (i >= total) {
   547     public Stream<? extends ZipEntry> stream() {
   493                             throw new NoSuchElementException();
   548         return StreamSupport.stream(Spliterators.spliterator(
   494                         }
   549                 new ZipEntryIterator(), size(),
   495                         long jzentry = getNextEntry(jzfile, i++);
   550                 Spliterator.ORDERED | Spliterator.DISTINCT |
   496                         if (jzentry == 0) {
   551                         Spliterator.IMMUTABLE | Spliterator.NONNULL));
   497                             String message;
       
   498                             if (closeRequested) {
       
   499                                 message = "ZipFile concurrently closed";
       
   500                             } else {
       
   501                                 message = getZipMessage(ZipFile.this.jzfile);
       
   502                             }
       
   503                             throw new ZipError("jzentry == 0" +
       
   504                                                ",\n jzfile = " + ZipFile.this.jzfile +
       
   505                                                ",\n total = " + ZipFile.this.total +
       
   506                                                ",\n name = " + ZipFile.this.name +
       
   507                                                ",\n i = " + i +
       
   508                                                ",\n message = " + message
       
   509                                 );
       
   510                         }
       
   511                         ZipEntry ze = getZipEntry(null, jzentry);
       
   512                         freeEntry(jzfile, jzentry);
       
   513                         return ze;
       
   514                     }
       
   515                 }
       
   516             };
       
   517     }
   552     }
   518 
   553 
   519     private ZipEntry getZipEntry(String name, long jzentry) {
   554     private ZipEntry getZipEntry(String name, long jzentry) {
   520         ZipEntry e = new ZipEntry();
   555         ZipEntry e = new ZipEntry();
   521         e.flag = getEntryFlag(jzentry);  // get the flag first
   556         e.flag = getEntryFlag(jzentry);  // get the flag first