PublicClassLoader.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1/*
2 * Copyright (C) 2008 The Android Open Source Project
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
17package org.apache.harmony.luni.tests.java.lang;
18
19import java.io.File;
20import java.io.FileInputStream;
21import java.io.IOException;
22import java.io.InputStream;
23
24    class PublicClassLoader extends ClassLoader {
25
26        public PublicClassLoader() {
27            super();
28        }
29
30        public PublicClassLoader(ClassLoader cl) {
31            super(cl);
32        }
33
34        private byte[] getBytes( String filename ) throws IOException {
35
36            File file = new File( filename );
37            long len = file.length();
38            byte raw[] = new byte[(int)len];
39            FileInputStream fin = new FileInputStream( file );
40            int r = fin.read( raw );
41            if (r != len)
42              throw new IOException( "Can't read all, "+r+" != "+len );
43            fin.close();
44            return raw;
45          }
46
47        public Class<?> loadClass(String name, boolean resolve)
48                                            throws ClassNotFoundException {
49
50            Class clazz = findLoadedClass(name);
51            String classFileName = name.replace( '.', '/' ) + ".class";
52            File classFile = new File(classFileName);
53            if (classFile.exists()) {
54                try {
55                    byte raw[] = getBytes(classFileName);
56                    clazz = defineClass( name, raw, 0, raw.length );
57                } catch(Exception ioe) {}
58            }
59
60            if (clazz == null) {
61                Package p = getClass().getPackage();
62                InputStream is = getResourceAsStream("/" + classFileName);
63                byte[] buf = new byte[512];
64                int len;
65                try {
66                    len = is.read(buf);
67                    clazz = defineClass(name, buf, 0, len);
68                } catch (IOException e) {
69                }
70            }
71
72            if (clazz == null) {
73                clazz = findSystemClass(name);
74            }
75
76            if(clazz == null)
77                throw new ClassNotFoundException(name);
78            return clazz;
79        }
80
81        public Class<?> defineClassTest(byte[] b, int off, int len) {
82            return defineClass(b, off, len);
83        }
84
85        public Package getPackage(String name) {
86            return super.getPackage(name);
87        }
88
89        public Package [] getPackages() {
90            return super.getPackages();
91        }
92
93        public InputStream getResourceAsStream(String name) {
94            return getClass().getResourceAsStream("/" + getClass().getPackage().
95                    getName().replace(".", "/") + "/" + name);
96        }
97    }
98