protected byte[] serializeToByteArray(Object o) throws IOException { _oo.writeObject(o); _oo.flush(); return _bao.toByteArray(); }
protected byte[] serializeToByteArray(Object o) throws IOException { _oo.writeObject(o); _oo.flush(); _zo.closeEntry(); return _bao.toByteArray(); }
/** * Serializes an object into a byte buffer. The object has to implement interface Serializable * or Externalizable. **/ public static byte[] objectToByteBuffer(Object obj) throws Exception { byte[] result = null; synchronized (outStream) { outStream.reset(); ObjectOutputStream out = new ObjectOutputStream(outStream); out.writeObject(obj); out.flush(); result = outStream.toByteArray(); out.close(); } return result; }
/** * Convert the desired object into the new object instance assignable from provided ClassLoader. * NOTE: This method is very expensive and provided object to convert must be {@link * java.io.Serializable}. * * @param obj the object to convert. Can be array as well. * @param classLoader the desired ClassLoader to convert the passed object. * @return the new instance of object derived from passed ClassLoader. * @throws IOException failed to convert **/ static public Object convertToAssignableClassLoader(Object obj, ClassLoader classLoader) throws IOException, ClassNotFoundException { GSByteArrayOutputStream oos = new GSByteArrayOutputStream(); MarshalOutputStream mos = new MarshalOutputStream(oos); mos.writeObject(obj); oos.flush(); GSByteArrayInputStream bais = new GSByteArrayInputStream(oos.toByteArray()); ClassLoader origCL = Thread.currentThread().getContextClassLoader(); try { ClassLoaderHelper.setContextClassLoader(classLoader, true); MarshalInputStream mis = new MarshalInputStream(bais); return mis.readObject(); } finally { ClassLoaderHelper.setContextClassLoader(origCL, true); } }