blob: 8f9ea4cc69a6fbfe0a86cb215dd8e58b188bc255 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package javax.management.loading;
27
28
29// java import
30
31import java.io.*;
32import java.lang.reflect.Array;
33
34
35/**
36 * This subclass of ObjectInputStream delegates loading of classes to
37 * an existing MLetClassLoader.
38 *
39 * @since 1.5
40 */
41class MLetObjectInputStream extends ObjectInputStream {
42
43 private MLet loader;
44
45 /**
46 * Loader must be non-null;
47 */
48 public MLetObjectInputStream(InputStream in, MLet loader)
49 throws IOException, StreamCorruptedException {
50
51 super(in);
52 if (loader == null) {
53 throw new IllegalArgumentException("Illegal null argument to MLetObjectInputStream");
54 }
55 this.loader = loader;
56 }
57
58 private Class primitiveType(char c) {
59 switch(c) {
60 case 66: /* 'B' */
61 return Byte.TYPE;
62
63 case 67: /* 'C' */
64 return Character.TYPE;
65
66 case 68: /* 'D' */
67 return Double.TYPE;
68
69 case 70: /* 'F' */
70 return Float.TYPE;
71
72 case 73: /* 'I' */
73 return Integer.TYPE;
74
75 case 74: /* 'J' */
76 return Long.TYPE;
77
78 case 83: /* 'S' */
79 return Short.TYPE;
80
81 case 90: /* 'Z' */
82 return Boolean.TYPE;
83 }
84 return null;
85 }
86
87 /**
88 * Use the given ClassLoader rather than using the system class
89 */
90 protected Class resolveClass(ObjectStreamClass objectstreamclass)
91 throws IOException, ClassNotFoundException {
92
93 String s = objectstreamclass.getName();
94 if (s.startsWith("[")) {
95 int i;
96 for (i = 1; s.charAt(i) == '['; i++);
97 Class class1;
98 if (s.charAt(i) == 'L') {
99 class1 = loader.loadClass(s.substring(i + 1, s.length() - 1));
100 } else {
101 if (s.length() != i + 1)
102 throw new ClassNotFoundException(s);
103 class1 = primitiveType(s.charAt(i));
104 }
105 int ai[] = new int[i];
106 for (int j = 0; j < i; j++)
107 ai[j] = 0;
108
109 return Array.newInstance(class1, ai).getClass();
110 } else {
111 return loader.loadClass(s);
112 }
113 }
114
115 /**
116 * Returns the ClassLoader being used
117 */
118 public ClassLoader getClassLoader() {
119 return loader;
120 }
121}