KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > io > ClassFileReader


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.io;
9
10 import org.gjt.jclasslib.structures.*;
11
12 import java.io.*;
13 import java.util.jar.JarEntry JavaDoc;
14 import java.util.jar.JarFile JavaDoc;
15
16 /**
17     Converts class files to a class file structure <tt>ClassFile</tt> as defined in
18     <tt>org.gjt.jclasslib.structures</tt>.
19
20     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
21     @version $Revision: 1.7 $ $Date: 2003/08/18 07:58:12 $
22 */

23 public class ClassFileReader {
24
25     private ClassFileReader() {
26     }
27
28     /**
29         Looks up a class file in the specified class path and converts it
30         to a <tt>ClassFile</tt> structure.
31         @param classPath the class path from which to read the <tt>ClassFile</tt> structure
32         @param packageName the name of the package in which the class resides
33         @param className the simple name of the class
34         @return the new <tt>ClassFile</tt> structure or <tt>null</tt> if it cannot be found
35         @throws InvalidByteCodeException if the code is invalid
36         @throws IOException if an exception occurs while reading the file
37      */

38     public static ClassFile readFromClassPath(String JavaDoc[] classPath, String JavaDoc packageName, String JavaDoc className)
39         throws InvalidByteCodeException, IOException
40     {
41         
42         String JavaDoc relativePath = packageName.replace('.', File.separatorChar) + (packageName.length() == 0 ? "" : File.separator) + className + ".class";
43         String JavaDoc jarRelativePath = relativePath.replace(File.separatorChar, '/');
44         for (int i = 0; i < classPath.length; i++) {
45             File JavaDoc currentClassPathEntry = new File JavaDoc(classPath[i]);
46             if (!currentClassPathEntry.exists()) {
47                 continue;
48             }
49             if (currentClassPathEntry.isDirectory()) {
50                 File JavaDoc testFile = new File JavaDoc(currentClassPathEntry, relativePath);
51                 if (testFile.exists()) {
52                     return readFromFile(testFile);
53                 }
54             } else if (currentClassPathEntry.isFile()) {
55                 JarFile JavaDoc jarFile = new JarFile JavaDoc(currentClassPathEntry);
56                 try {
57                     JarEntry JavaDoc jarEntry = jarFile.getJarEntry(jarRelativePath);
58                     if (jarEntry != null) {
59                         return readFromInputStream(jarFile.getInputStream(jarEntry));
60                     }
61                 } finally {
62                     jarFile.close();
63                 }
64             }
65         }
66
67         return null;
68     }
69
70     /**
71         Converts a class file to a <tt>ClassFile</tt> structure.
72         @param file the file from which to read the <tt>ClassFile</tt> structure
73         @return the new <tt>ClassFile</tt> structure
74         @throws InvalidByteCodeException if the code is invalid
75         @throws IOException if an exception occurs while reading the file
76      */

77     public static ClassFile readFromFile(File JavaDoc file)
78         throws InvalidByteCodeException, IOException
79     {
80
81         return readFromInputStream(new FileInputStream(file));
82     }
83
84     /**
85         Converts a class file to a <tt>ClassFile</tt> structure.
86         @param is the input stream from which to read the
87                   <tt>ClassFile</tt> structure
88         @return the new <tt>ClassFile</tt> structure
89         @throws InvalidByteCodeException if the code is invalid
90         @throws IOException if an exception occurs while reading from
91                             the input stream
92      */

93     public static ClassFile readFromInputStream(InputStream is)
94         throws InvalidByteCodeException, IOException
95     {
96
97         DataInputStream in = new DataInputStream(
98                                 new BufferedInputStream(is));
99
100         ClassFile classFile = new ClassFile();
101         classFile.read(in);
102         in.close();
103         return classFile;
104     }
105
106     /**
107      * Test method.
108      * @param args arguments
109      * @throws Exception
110      */

111     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
112
113         final int maxCount = 500;
114         long startTime, endTime;
115
116         File JavaDoc file = new File JavaDoc(args[0]);
117         ClassFile classFile = readFromFile(file);
118
119         startTime = System.currentTimeMillis();
120         for (int i = 0; i < maxCount; i++) {
121             classFile = readFromFile(file);
122         }
123         endTime = System.currentTimeMillis();
124         System.out.println("With attributes:");
125         System.out.print((endTime - startTime));
126         System.out.println(" ms");
127
128         System.setProperty(AttributeInfo.SYSTEM_PROPERTY_SKIP_ATTRIBUTES, "true");
129         startTime = System.currentTimeMillis();
130         for (int i = 0; i < maxCount; i++) {
131             classFile = readFromFile(file);
132         }
133         endTime = System.currentTimeMillis();
134         System.out.println("Without attributes:");
135         System.out.print((endTime - startTime));
136         System.out.println(" ms");
137
138     }
139
140 }
141
Popular Tags