001: /*
002: * @(#)AppletObjectInputStream.java 1.15 06/10/16
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package sun.applet;
028:
029: import java.io.*;
030: import java.lang.reflect.Array;
031:
032: /**
033: * This subclass of ObjectInputStream delegates loading of classes to
034: * an existing ClassLoader.
035: */
036:
037: class AppletObjectInputStream extends ObjectInputStream {
038: private AppletClassLoader loader;
039:
040: /**
041: * Loader must be non-null;
042: */
043:
044: public AppletObjectInputStream(InputStream in,
045: AppletClassLoader loader) throws IOException,
046: StreamCorruptedException {
047: super (in);
048: if (loader == null) {
049: throw new AppletIllegalArgumentException(
050: "appletillegalargumentexception.objectinputstream");
051: }
052: this .loader = loader;
053: }
054:
055: /**
056: * Make a primitive array class
057: */
058:
059: private Class primitiveType(char type) {
060: switch (type) {
061: case 'B':
062: return byte.class;
063:
064: case 'C':
065: return char.class;
066:
067: case 'D':
068: return double.class;
069:
070: case 'F':
071: return float.class;
072:
073: case 'I':
074: return int.class;
075:
076: case 'J':
077: return long.class;
078:
079: case 'S':
080: return short.class;
081:
082: case 'Z':
083: return boolean.class;
084:
085: default:
086: return null;
087: }
088: }
089:
090: /**
091: * Use the given ClassLoader rather than using the system class
092: */
093: protected Class resolveClass(ObjectStreamClass classDesc)
094: throws IOException, ClassNotFoundException {
095: String cname = classDesc.getName();
096: if (cname.startsWith("[")) {
097: // An array
098: Class component; // component class
099: int dcount; // dimension
100: for (dcount = 1; cname.charAt(dcount) == '['; dcount++)
101: ;
102: if (cname.charAt(dcount) == 'L') {
103: component = loader.loadClass(cname.substring(
104: dcount + 1, cname.length() - 1));
105: } else {
106: if (cname.length() != dcount + 1) {
107: throw new ClassNotFoundException(cname);// malformed
108: }
109: component = primitiveType(cname.charAt(dcount));
110: }
111: int dim[] = new int[dcount];
112: for (int i = 0; i < dcount; i++) {
113: dim[i] = 0;
114: }
115: return Array.newInstance(component, dim).getClass();
116: } else {
117: return loader.loadClass(cname);
118: }
119: }
120: }
|