/[classpath]/cp-tools/src/gnu/classpath/tools/Util.java
ViewVC logotype

Diff of /cp-tools/src/gnu/classpath/tools/Util.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by cbj, Sun Jan 30 02:21:06 2005 UTC revision 1.3 by cbj, Sun Jan 30 04:19:27 2005 UTC
# Line 31  import java.util.zip.ZipException; Line 31  import java.util.zip.ZipException;
31    
32  public class Util  public class Util
33  {  {
34    public static InputStream getInputStream(String filename, String classpath)    private String bootclasspath;
35      private String classpath;
36      private String userclasspath;
37    
38      public Util()
39      {
40        classpath = System.getProperty("java.class.path");
41        bootclasspath = System.getProperty("sun.boot.class.path");
42      }
43    
44      /**
45       * Searches from the current working directory for a file
46       * package/classname.class unless the user supplied a classpath. If so then
47       * searches through the user supplied classpath. Finally searches the system
48       * classpath and bootclasspath.
49       *
50       * @param className the class to find
51       * @return a valid InputStream for reading the given class's class file
52       * @throws ClassNotFoundException if the class cannot be found
53       */
54      public InputStream findClass(String className) throws ClassNotFoundException
55      {
56        InputStream is = null;
57        String filename = getFileNameFromClassName(className);
58        if (userclasspath != null)
59          {
60            String path = getSearchPath();
61            is = findClass(filename, path);
62          }
63        else
64          {
65            is = findFile(filename);
66          }
67        if (is != null)
68          return is;
69        throw new ClassNotFoundException(className + " not found");
70      }
71    
72      /**
73       * Attempt to get an InputStream for the specified filename.
74       *
75       * @param filename the filename to find in the specified path
76       * @param classpath the path or paths to search for the specified file
77       * @return <code>null</code> if the file cannot be found
78       */
79      private InputStream findClass(String filename, String classpath)
80    {    {
81      StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);      StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
82      while (st.hasMoreTokens())      while (st.hasMoreTokens())
# Line 78  public class Util Line 123  public class Util
123        }        }
124      return null;      return null;
125    }    }
 }  
126    
127      /**
128       * Attempt to get an InputStream for the specified filename.
129       *
130       * @param filename the filename to get an InputStream for
131       * @return <code>null</code> if the file cannot be found
132       */
133      private InputStream findFile(String filename)
134      {
135        File f = new File(filename);
136        if (f.exists())
137          {
138            try
139              {
140                FileInputStream fis = new FileInputStream(f);
141                return new BufferedInputStream(fis);
142              }
143            catch (FileNotFoundException fe)
144              {
145              }
146          }
147        return null;
148      }
149    
150      /**
151       * Class names of the form a.b.c, or simply c if the class has no package are
152       * the most common. Also support the use of filenames here, if the file exists
153       * relative to the current working directory or is absolute.
154       *
155       * @param className
156       * @return
157       */
158      private String getFileNameFromClassName(String className)
159      {
160        File f = new File(className);
161        if (f.exists())
162          return className;
163        String filename = className.replace('.', '/');
164        filename = filename + ".class";
165        return filename;
166      }
167    
168      /**
169       * Used to set the user provided classpath.
170       *
171       * @param path the user classpath
172       */
173      public void setClasspath(String path)
174      {
175        userclasspath = path;
176      }
177      
178      /**
179       * Returns the path used to find classes.  To make it possible to
180       * find classes from the user's provided classpath before finding
181       * them in the system classpath the user's path will be first instead
182       * of last as in Sun's implementation.
183       *
184       * @return the entire classpath
185       */
186      public String getSearchPath()
187      {
188        StringBuffer buf = new StringBuffer();
189        if (userclasspath != null)
190          buf.append(userclasspath);
191        else
192        {
193          if (bootclasspath != null)
194          {
195            buf.append(bootclasspath);
196            buf.append(File.pathSeparator);
197          }
198          buf.append(classpath);
199        }
200        return buf.toString();    
201      }
202    }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26