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 
  39 class AppletObjectInputStream extends ObjectInputStream
  40 {
  41     private AppletClassLoader loader;
  42 
  43     /**
  44      * Loader must be non-null;
  45      */
  46 
  47     public AppletObjectInputStream(InputStream in, AppletClassLoader loader)
  48             throws IOException, StreamCorruptedException {
  49 
  50         super(in);
  51         if (loader == null) {
  52             throw new AppletIllegalArgumentException("appletillegalargumentexception.objectinputstream");
  53 
  54         }
  55         this.loader = loader;
  56     }
  57 
  58     /**
  59      * Make a primitive array class
  60      */
  61 
  62     private Class<?> primitiveType(char type) {
  63         switch (type) {
  64         case 'B': return byte.class;
  65         case 'C': return char.class;
  66         case 'D': return double.class;
  67         case 'F': return float.class;
  68         case 'I': return int.class;
  69         case 'J': return long.class;
  70         case 'S': return short.class;
  71         case 'Z': return boolean.class;
  72         default: return null;
  73         }
  74     }
  75 
  76     /**
  77      * Use the given ClassLoader rather than using the system class
  78      */
  79     protected Class<?> resolveClass(ObjectStreamClass classDesc)
  80         throws IOException, ClassNotFoundException {
  81 
  82         String cname = classDesc.getName();
  83         if (cname.startsWith("[")) {
  84             // An array
  85             Class<?> component;            // component class
  86             int dcount;                 // dimension
  87             for (dcount=1; cname.charAt(dcount)=='['; dcount++) ;
  88             if (cname.charAt(dcount) == 'L') {
  89                 component = loader.loadClass(cname.substring(dcount+1,
  90                                                              cname.length()-1));
  91             } else {
  92                 if (cname.length() != dcount+1) {
  93                     throw new ClassNotFoundException(cname);// malformed
  94                 }
  95                 component = primitiveType(cname.charAt(dcount));
  96             }
  97             int dim[] = new int[dcount];
  98             for (int i=0; i<dcount; i++) {
  99                 dim[i]=0;
 100             }
 101             return Array.newInstance(component, dim).getClass();
 102         } else {
 103             return loader.loadClass(cname);
 104         }
 105     }
 106 }