1 19 package org.openide.util.io; 20 21 import org.openide.util.Lookup; 22 import org.openide.util.Utilities; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.ObjectInput ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectStreamClass ; 30 31 import java.lang.reflect.InvocationTargetException ; 32 import org.openide.util.Exceptions; 33 34 35 37 45 public class NbObjectInputStream extends ObjectInputStream { 46 50 public NbObjectInputStream(InputStream is) throws IOException { 51 super(is); 52 53 try { 54 enableResolveObject(true); 55 } catch (SecurityException ex) { 56 throw new IOException (ex.toString()); 57 } 58 } 59 60 63 protected Class resolveClass(ObjectStreamClass v) throws IOException , ClassNotFoundException { 64 ClassLoader cl = getNBClassLoader(); 65 66 try { 67 return Class.forName(v.getName(), false, cl); 68 } catch (ClassNotFoundException cnfe) { 69 String msg = "Offending classloader: " + cl; Exceptions.attachMessage(cnfe, msg); 71 throw cnfe; 72 } 73 } 74 75 76 private static ClassLoader getNBClassLoader() { 77 ClassLoader c = Lookup.getDefault().lookup(ClassLoader .class); 78 79 return (c != null) ? c : ClassLoader.getSystemClassLoader(); 80 } 81 82 114 protected ObjectStreamClass readClassDescriptor() throws IOException , ClassNotFoundException { 115 ObjectStreamClass ose = super.readClassDescriptor(); 116 117 String name = ose.getName(); 118 String newN = Utilities.translate(name); 119 120 if (name == newN) { 121 return ose; 123 } 124 125 ClassLoader cl = getNBClassLoader(); 127 Class clazz = Class.forName(newN, false, cl); 128 129 ObjectStreamClass newOse = ObjectStreamClass.lookup(clazz); 130 131 if (newOse == null) { 134 throw new java.io.NotSerializableException (newN); 135 } 136 137 return newOse; 138 } 139 140 149 public static Object readSafely(ObjectInput oi) throws IOException { 150 int size = oi.readInt(); 151 byte[] byteArray = new byte[size]; 152 oi.readFully(byteArray, 0, size); 153 154 try { 155 ByteArrayInputStream bis = new ByteArrayInputStream (byteArray); 156 NbObjectInputStream ois = new NbObjectInputStream(bis); 157 Object obj = ois.readObject(); 158 bis.close(); 159 160 return obj; 161 } catch (Exception exc) { 162 throw new SafeException(exc); 164 } catch (LinkageError le) { 165 throw new SafeException(new InvocationTargetException (le)); 166 } 167 } 168 169 175 public static void skipSafely(ObjectInput oi) throws IOException { 176 int size = oi.readInt(); 177 oi.skip(size); 178 } 179 } 180 | Popular Tags |