FileDocCategorySizeDatePackage
JarFileIterator.javaAPI DocApache Ant 1.702808Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs.optional.depend

JarFileIterator

public class JarFileIterator extends Object implements ClassFileIterator
A class file iterator which iterates through the contents of a Java jar file.

Fields Summary
private ZipInputStream
jarStream
The jar stream from the jar file being iterated over
Constructors Summary
public JarFileIterator(InputStream stream)
Construct an iterator over a jar stream

param
stream the basic input stream from which the Jar is received
exception
IOException if the jar stream cannot be created

        super();

        jarStream = new ZipInputStream(stream);
    
Methods Summary
public ClassFilegetNextClassFile()
Get the next ClassFile object from the jar

return
a ClassFile object describing the class from the jar

        ZipEntry jarEntry;
        ClassFile nextElement = null;

        try {
            jarEntry = jarStream.getNextEntry();

            while (nextElement == null && jarEntry != null) {
                String entryName = jarEntry.getName();

                if (!jarEntry.isDirectory() && entryName.endsWith(".class")) {

                    // create a data input stream from the jar input stream
                    ClassFile javaClass = new ClassFile();

                    javaClass.read(jarStream);

                    nextElement = javaClass;
                } else {

                    jarEntry = jarStream.getNextEntry();
                }
            }
        } catch (IOException e) {
            String message = e.getMessage();
            String text = e.getClass().getName();

            if (message != null) {
                text += ": " + message;
            }

            throw new RuntimeException("Problem reading JAR file: " + text);
        }

        return nextElement;