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

@@ -27,10 +27,12 @@
 
 import java.io.*;
 import java.lang.ref.SoftReference;
 import java.net.URL;
 import java.util.*;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
 import java.util.zip.*;
 import java.security.CodeSigner;
 import java.security.cert.Certificate;
 import java.security.AccessController;
 import java.security.CodeSource;

@@ -233,24 +235,45 @@
             return new JarFileEntry(ze);
         }
         return null;
     }
 
+    protected class JarEntryIterator implements Enumeration<JarEntry>,
+            Iterator<JarEntry>
+    {
+        final Iterator<ZipEntry> e = new ZipFile.ZipEntryIterator();
+
+        public boolean hasMoreElements() {
+            return e.hasNext();
+        }
+
+        public JarEntry nextElement() {
+            return next();
+        }
+
+        public boolean hasNext() {
+            return e.hasNext();
+        }
+
+        public JarEntry next() {
+            ZipEntry ze = e.next();
+            return new JarFileEntry(ze);
+        }
+    }
+
     /**
      * Returns an enumeration of the zip file entries.
      */
     public Enumeration<JarEntry> entries() {
-        final Enumeration<? extends ZipEntry> enum_ = super.entries();
-        return new Enumeration<JarEntry>() {
-            public boolean hasMoreElements() {
-                return enum_.hasMoreElements();
-            }
-            public JarFileEntry nextElement() {
-                ZipEntry ze = enum_.nextElement();
-                return new JarFileEntry(ze);
+        return new JarEntryIterator();
             }
-        };
+
+    @Override
+    public Stream<JarEntry> stream() {
+        return StreamSupport.stream(Spliterators.spliterator(
+                new JarEntryIterator(), size(),
+                Spliterator.DISTINCT | Spliterator.IMMUTABLE | Spliterator.NONNULL));
     }
 
     private class JarFileEntry extends JarEntry {
         JarFileEntry(ZipEntry ze) {
             super(ze);