1 /*
   2  * Copyright (c) 1996, 1997, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 /*
  26  * COPYRIGHT goes here
  27  */
  28 
  29 package sun.applet;
  30 
  31 import java.io.*;
  32 import java.lang.reflect.Array;
  33 
  34 /**
  35  * This subclass of ObjectInputStream delegates loading of classes to
  36  * an existing ClassLoader.
  37  *
  38  * @deprecated The Applet API is deprecated. See the
  39  * <a href="../../java/applet/package-summary.html"> java.applet package
  40  * documentation</a> for further information.
  41  */
  42 @Deprecated(since = "9")
  43 class AppletObjectInputStream extends ObjectInputStream
  44 {
  45     private AppletClassLoader loader;
  46 
  47     /**
  48      * Loader must be non-null;
  49      */
  50 
  51     public AppletObjectInputStream(InputStream in, AppletClassLoader loader)
  52             throws IOException, StreamCorruptedException {
  53 
  54         super(in);
  55         if (loader == null) {
  56             throw new AppletIllegalArgumentException("appletillegalargumentexception.objectinputstream");
  57 
  58         }
  59         this.loader = loader;
  60     }
  61 
  62     /**
  63      * Make a primitive array class
  64      */
  65 
  66     private Class<?> primitiveType(char type) {
  67         switch (type) {
  68         case 'B': return byte.class;
  69         case 'C': return char.class;
  70         case 'D': return double.class;
  71         case 'F': return float.class;
  72         case 'I': return int.class;
  73         case 'J': return long.class;
  74         case 'S': return short.class;
  75         case 'Z': return boolean.class;
  76         default: return null;
  77         }
  78     }
  79 
  80     /**
  81      * Use the given ClassLoader rather than using the system class
  82      */
  83     protected Class<?> resolveClass(ObjectStreamClass classDesc)
  84         throws IOException, ClassNotFoundException {
  85 
  86         String cname = classDesc.getName();
  87         if (cname.startsWith("[")) {
  88             // An array
  89             Class<?> component;            // component class
  90             int dcount;                 // dimension
  91             for (dcount=1; cname.charAt(dcount)=='['; dcount++) ;
  92             if (cname.charAt(dcount) == 'L') {
  93                 component = loader.loadClass(cname.substring(dcount+1,
  94                                                              cname.length()-1));
  95             } else {
  96                 if (cname.length() != dcount+1) {
  97                     throw new ClassNotFoundException(cname);// malformed
  98                 }
  99                 component = primitiveType(cname.charAt(dcount));
 100             }
 101             int dim[] = new int[dcount];
 102             for (int i=0; i<dcount; i++) {
 103                 dim[i]=0;
 104             }
 105             return Array.newInstance(component, dim).getClass();
 106         } else {
 107             return loader.loadClass(cname);
 108         }
 109     }
 110 }