KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cojen > classfile > ResourceClassFileDataLoader


1 /*
2  * Copyright 2004 Brian S O'Neill
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.cojen.classfile;
18
19 import java.io.InputStream JavaDoc;
20
21 /**
22  * {@link ClassFileDataLoader} implementation that loads class file data as
23  * resources from a ClassLoader.
24  *
25  * @author Brian S O'Neill
26  */

27 public class ResourceClassFileDataLoader implements ClassFileDataLoader {
28     private final ClassLoader JavaDoc mLoader;
29
30     /**
31      * Loads resources using the ClassLoader that loaded this class.
32      */

33     public ResourceClassFileDataLoader() {
34         mLoader = getClass().getClassLoader();
35     }
36
37     /**
38      * @param loader ClassLoader for finding resources; if null, use system
39      * ClassLoader.
40      */

41     public ResourceClassFileDataLoader(ClassLoader JavaDoc loader) {
42         mLoader = loader;
43     }
44
45     public InputStream JavaDoc getClassData(String JavaDoc name) {
46         name = name.replace('.', '/') + ".class";
47
48         InputStream JavaDoc in;
49         if (mLoader == null) {
50             in = ClassLoader.getSystemResourceAsStream(name);
51         } else {
52             in = mLoader.getResourceAsStream(name);
53         }
54
55         return in;
56     }
57 }
58
Popular Tags