KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > depend > JarFileIterator


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.depend;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.util.zip.ZipEntry JavaDoc;
23 import java.util.zip.ZipInputStream JavaDoc;
24
25 /**
26  * A class file iterator which iterates through the contents of a Java jar
27  * file.
28  *
29  */

30 public class JarFileIterator implements ClassFileIterator {
31     /** The jar stream from the jar file being iterated over*/
32     private ZipInputStream JavaDoc jarStream;
33
34     /**
35      * Construct an iterator over a jar stream
36      *
37      * @param stream the basic input stream from which the Jar is received
38      * @exception IOException if the jar stream cannot be created
39      */

40     public JarFileIterator(InputStream JavaDoc stream) throws IOException JavaDoc {
41         super();
42
43         jarStream = new ZipInputStream JavaDoc(stream);
44     }
45
46     /**
47      * Get the next ClassFile object from the jar
48      *
49      * @return a ClassFile object describing the class from the jar
50      */

51     public ClassFile getNextClassFile() {
52         ZipEntry JavaDoc jarEntry;
53         ClassFile nextElement = null;
54
55         try {
56             jarEntry = jarStream.getNextEntry();
57
58             while (nextElement == null && jarEntry != null) {
59                 String JavaDoc entryName = jarEntry.getName();
60
61                 if (!jarEntry.isDirectory() && entryName.endsWith(".class")) {
62
63                     // create a data input stream from the jar input stream
64
ClassFile javaClass = new ClassFile();
65
66                     javaClass.read(jarStream);
67
68                     nextElement = javaClass;
69                 } else {
70
71                     jarEntry = jarStream.getNextEntry();
72                 }
73             }
74         } catch (IOException JavaDoc e) {
75             String JavaDoc message = e.getMessage();
76             String JavaDoc text = e.getClass().getName();
77
78             if (message != null) {
79                 text += ": " + message;
80             }
81
82             throw new RuntimeException JavaDoc("Problem reading JAR file: " + text);
83         }
84
85         return nextElement;
86     }
87
88 }
89
90
Popular Tags