src/share/classes/java/util/zip/ZipFile.java
Print this page
rev 7020 : 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
Contributed-by: akhil.arora@oracle.com, brian.goetz@oracle.com
@@ -34,15 +34,19 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Enumeration;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
+import java.util.Spliterator;
+import java.util.Spliterators;
import java.util.WeakHashMap;
-import java.security.AccessController;
-import sun.security.action.GetPropertyAction;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
import static java.util.zip.ZipConstants64.*;
/**
* This class is used to read entries from a zip file.
*
@@ -469,26 +473,33 @@
*/
public String getName() {
return name;
}
- /**
- * Returns an enumeration of the ZIP file entries.
- * @return an enumeration of the ZIP file entries
- * @throws IllegalStateException if the zip file has been closed
- */
- public Enumeration<? extends ZipEntry> entries() {
- ensureOpen();
- return new Enumeration<ZipEntry>() {
+ protected class ZipEntryIterator implements Enumeration<ZipEntry>, Iterator<ZipEntry> {
private int i = 0;
+
+ public ZipEntryIterator() {
+ ensureOpen();
+ }
+
public boolean hasMoreElements() {
+ return hasNext();
+ }
+
+ public boolean hasNext() {
synchronized (ZipFile.this) {
ensureOpen();
return i < total;
}
}
- public ZipEntry nextElement() throws NoSuchElementException {
+
+ public ZipEntry nextElement() {
+ return next();
+ }
+
+ public ZipEntry next() {
synchronized (ZipFile.this) {
ensureOpen();
if (i >= total) {
throw new NoSuchElementException();
}
@@ -511,11 +522,32 @@
ZipEntry ze = getZipEntry(null, jzentry);
freeEntry(jzfile, jzentry);
return ze;
}
}
- };
+ }
+
+ /**
+ * Returns an enumeration of the ZIP file entries.
+ * @return an enumeration of the ZIP file entries
+ * @throws IllegalStateException if the zip file has been closed
+ */
+ public Enumeration<? extends ZipEntry> entries() {
+ return new ZipEntryIterator();
+ }
+
+ /**
+ * Return a {@code Stream} of the ZIP file entries.
+ *
+ * @return a {@code Stream} of entries in this ZIP file
+ * @throws IllegalStateException if the zip file has been closed
+ * @since 1.8
+ */
+ public Stream<? extends ZipEntry> stream() {
+ return StreamSupport.stream(Spliterators.spliterator(
+ new ZipEntryIterator(), size(),
+ Spliterator.DISTINCT | Spliterator.IMMUTABLE | Spliterator.NONNULL));
}
private ZipEntry getZipEntry(String name, long jzentry) {
ZipEntry e = new ZipEntry();
e.flag = getEntryFlag(jzentry); // get the flag first