X-Git-Url: https://juplo.de/gitweb/?p=scannotation;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fscannotation%2Farchiveiterator%2FJarIterator.java;fp=src%2Fmain%2Fjava%2Forg%2Fscannotation%2Farchiveiterator%2FJarIterator.java;h=67291a6e5c74786b58e7bcd0695c877d94ec5c80;hp=0000000000000000000000000000000000000000;hb=36e6637926203201648e7892ec6ee1240807218e;hpb=58b6663aae5313b41167d92851981ca549cbb461 diff --git a/src/main/java/org/scannotation/archiveiterator/JarIterator.java b/src/main/java/org/scannotation/archiveiterator/JarIterator.java new file mode 100644 index 0000000..67291a6 --- /dev/null +++ b/src/main/java/org/scannotation/archiveiterator/JarIterator.java @@ -0,0 +1,77 @@ +package org.scannotation.archiveiterator; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public class JarIterator implements StreamIterator +{ + JarInputStream jar; + JarEntry next; + Filter filter; + boolean initial = true; + boolean closed = false; + + public JarIterator(File file, Filter filter) throws IOException + { + this(new FileInputStream(file), filter); + } + + + public JarIterator(InputStream is, Filter filter) throws IOException + { + this.filter = filter; + jar = new JarInputStream(is); + } + + private void setNext() + { + initial = true; + try + { + if (next != null) jar.closeEntry(); + next = null; + do + { + next = jar.getNextJarEntry(); + } while (next != null && (next.isDirectory() || (filter == null || !filter.accepts(next.getName())))); + if (next == null) + { + close(); + } + } + catch (IOException e) + { + throw new RuntimeException("failed to browse jar", e); + } + } + + public InputStream next() + { + if (closed || (next == null && !initial)) return null; + setNext(); + if (next == null) return null; + return new InputStreamWrapper(jar); + } + + public void close() + { + try + { + closed = true; + jar.close(); + } + catch (IOException ignored) + { + + } + + } +}