01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
21: import java.io.InputStream;
22: import java.util.zip.ZipEntry;
23: import java.util.zip.ZipInputStream;
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 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 stream) throws IOException {
41: super ();
42:
43: jarStream = new ZipInputStream(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 jarEntry;
53: ClassFile nextElement = null;
54:
55: try {
56: jarEntry = jarStream.getNextEntry();
57:
58: while (nextElement == null && jarEntry != null) {
59: String entryName = jarEntry.getName();
60:
61: if (!jarEntry.isDirectory()
62: && entryName.endsWith(".class")) {
63:
64: // create a data input stream from the jar input stream
65: ClassFile javaClass = new ClassFile();
66:
67: javaClass.read(jarStream);
68:
69: nextElement = javaClass;
70: } else {
71:
72: jarEntry = jarStream.getNextEntry();
73: }
74: }
75: } catch (IOException e) {
76: String message = e.getMessage();
77: String text = e.getClass().getName();
78:
79: if (message != null) {
80: text += ": " + message;
81: }
82:
83: throw new RuntimeException("Problem reading JAR file: "
84: + text);
85: }
86:
87: return nextElement;
88: }
89:
90: }
|