1 | /*
|
---|
2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.optional.depend;
|
---|
18 |
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.io.InputStream;
|
---|
21 | import java.util.zip.ZipEntry;
|
---|
22 | import java.util.zip.ZipInputStream;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * A class file iterator which iterates through the contents of a Java jar
|
---|
26 | * file.
|
---|
27 | *
|
---|
28 | */
|
---|
29 | public class JarFileIterator implements ClassFileIterator {
|
---|
30 | /** The jar stream from the jar file being iterated over*/
|
---|
31 | private ZipInputStream jarStream;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Construct an iterator over a jar stream
|
---|
35 | *
|
---|
36 | * @param stream the basic input stream from which the Jar is received
|
---|
37 | * @exception IOException if the jar stream cannot be created
|
---|
38 | */
|
---|
39 | public JarFileIterator(InputStream stream) throws IOException {
|
---|
40 | super();
|
---|
41 |
|
---|
42 | jarStream = new ZipInputStream(stream);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Get the next ClassFile object from the jar
|
---|
47 | *
|
---|
48 | * @return a ClassFile object describing the class from the jar
|
---|
49 | */
|
---|
50 | public ClassFile getNextClassFile() {
|
---|
51 | ZipEntry jarEntry;
|
---|
52 | ClassFile nextElement = null;
|
---|
53 |
|
---|
54 | try {
|
---|
55 | jarEntry = jarStream.getNextEntry();
|
---|
56 |
|
---|
57 | while (nextElement == null && jarEntry != null) {
|
---|
58 | String entryName = jarEntry.getName();
|
---|
59 |
|
---|
60 | if (!jarEntry.isDirectory() && entryName.endsWith(".class")) {
|
---|
61 |
|
---|
62 | // create a data input stream from the jar input stream
|
---|
63 | ClassFile javaClass = new ClassFile();
|
---|
64 |
|
---|
65 | javaClass.read(jarStream);
|
---|
66 |
|
---|
67 | nextElement = javaClass;
|
---|
68 | } else {
|
---|
69 |
|
---|
70 | jarEntry = jarStream.getNextEntry();
|
---|
71 | }
|
---|
72 | }
|
---|
73 | } catch (IOException e) {
|
---|
74 | String message = e.getMessage();
|
---|
75 | String text = e.getClass().getName();
|
---|
76 |
|
---|
77 | if (message != null) {
|
---|
78 | text += ": " + message;
|
---|
79 | }
|
---|
80 |
|
---|
81 | throw new RuntimeException("Problem reading JAR file: " + text);
|
---|
82 | }
|
---|
83 |
|
---|
84 | return nextElement;
|
---|
85 | }
|
---|
86 |
|
---|
87 | }
|
---|
88 |
|
---|