KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > loading > MLetObjectInputStream


1 /*
2  * @(#)MLetObjectInputStream.java 1.14 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management.loading;
9
10
11 // java import
12

13 import java.io.*;
14 import java.lang.reflect.Array JavaDoc;
15
16
17 /**
18  * This subclass of ObjectInputStream delegates loading of classes to
19  * an existing MLetClassLoader.
20  *
21  * @since 1.5
22  */

23 class MLetObjectInputStream extends ObjectInputStream {
24
25     private MLet JavaDoc loader;
26     
27     /**
28      * Loader must be non-null;
29      */

30     public MLetObjectInputStream(InputStream in, MLet JavaDoc loader)
31         throws IOException, StreamCorruptedException {
32
33         super(in);
34         if (loader == null) {
35             throw new IllegalArgumentException JavaDoc("Illegal null argument to MLetObjectInputStream");
36         }
37         this.loader = loader;
38     }
39
40     private Class JavaDoc primitiveType(char c) {
41         switch(c) {
42         case 66: /* 'B' */
43             return Byte.TYPE;
44
45         case 67: /* 'C' */
46             return Character.TYPE;
47
48         case 68: /* 'D' */
49             return Double.TYPE;
50
51         case 70: /* 'F' */
52             return Float.TYPE;
53
54         case 73: /* 'I' */
55             return Integer.TYPE;
56
57         case 74: /* 'J' */
58             return Long.TYPE;
59
60         case 83: /* 'S' */
61             return Short.TYPE;
62
63         case 90: /* 'Z' */
64             return Boolean.TYPE;
65         }
66         return null;
67     }
68
69     /**
70      * Use the given ClassLoader rather than using the system class
71      */

72     protected Class JavaDoc resolveClass(ObjectStreamClass objectstreamclass)
73         throws IOException, ClassNotFoundException JavaDoc {
74
75         String JavaDoc s = objectstreamclass.getName();
76         if (s.startsWith("[")) {
77             int i;
78             for (i = 1; s.charAt(i) == '['; i++);
79             Class JavaDoc class1;
80             if (s.charAt(i) == 'L') {
81                 class1 = loader.loadClass(s.substring(i + 1, s.length() - 1));
82             } else {
83                 if (s.length() != i + 1)
84                     throw new ClassNotFoundException JavaDoc(s);
85                 class1 = primitiveType(s.charAt(i));
86             }
87             int ai[] = new int[i];
88             for (int j = 0; j < i; j++)
89                 ai[j] = 0;
90
91             return Array.newInstance(class1, ai).getClass();
92         } else {
93             return loader.loadClass(s);
94         }
95     }
96
97     /**
98      * Returns the ClassLoader being used
99      */

100     public ClassLoader JavaDoc getClassLoader() {
101         return loader;
102     }
103 }
104
Popular Tags