home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / ObjectInputStreamWithLoader.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  2.0 KB  |  83 lines

  1. package sun.beanbox;
  2.  
  3. import java.io.*;
  4. import java.lang.reflect.Array;
  5.  
  6. /**
  7.  * This subclass of ObjectInputStream delegates loading of classes to
  8.  * an existing ClassLoader.
  9.  */
  10.  
  11. public class ObjectInputStreamWithLoader extends ObjectInputStream
  12. {
  13.     private ClassLoader loader;
  14.  
  15.     /**
  16.      * Loader must be non-null;
  17.      */
  18.  
  19.     public ObjectInputStreamWithLoader(InputStream in, ClassLoader loader)
  20.         throws IOException, StreamCorruptedException {
  21.  
  22.     super(in);
  23.     if (loader == null || in == null) {
  24.             throw new IllegalArgumentException("Illegal null argument to ObjectInputStreamWithLoader");
  25.     }
  26.     this.loader = loader;
  27.     }
  28.  
  29.     /**
  30.      * Make a primitive array class
  31.      */
  32.  
  33.     private Class primitiveType(char type) {
  34.     switch (type) {
  35.     case 'B': return byte.class;
  36.         case 'C': return char.class;
  37.     case 'D': return double.class;
  38.     case 'F': return float.class;
  39.     case 'I': return int.class;
  40.     case 'J': return long.class;
  41.     case 'S': return short.class;
  42.     case 'Z': return boolean.class;
  43.     default: return null;
  44.     }
  45.     }
  46.  
  47.     /**
  48.      * Use the given ClassLoader rather than using the system class
  49.      */
  50.     protected Class resolveClass(ObjectStreamClass classDesc)
  51.     throws IOException, ClassNotFoundException {
  52.  
  53.     String cname = classDesc.getName();
  54.     if (cname.startsWith("[")) {
  55.         // An array
  56.         Class component;        // component class
  57.         int dcount;            // dimension
  58.         for (dcount=1; cname.charAt(dcount)=='['; dcount++) ;
  59.         if (cname.charAt(dcount) == 'L') {
  60.         component = loader.loadClass(cname.substring(dcount+1,
  61.                                  cname.length()-1));
  62.         } else {
  63.         if (cname.length() != dcount+1) {
  64.             throw new ClassNotFoundException(cname);// malformed
  65.         }
  66.         component = primitiveType(cname.charAt(dcount));
  67.         }
  68.         int dim[] = new int[dcount];
  69.         for (int i=0; i<dcount; i++) {
  70.         dim[i]=0;
  71.         }
  72.         return Array.newInstance(component, dim).getClass();
  73.     } else {
  74.         return loader.loadClass(cname);
  75.     }
  76.     }
  77.  
  78.  
  79.     public ClassLoader getClassLoader() {
  80.     return loader;
  81.     }
  82. }
  83.