/** * Deserialized with ClassLoader */ public static Object javaDeserializeWithCL(byte[] serialized, URLClassLoader loader) { try { ByteArrayInputStream bis = new ByteArrayInputStream(serialized); Object ret; if (loader != null) { ClassLoaderObjectInputStream cis = new ClassLoaderObjectInputStream(loader, bis); ret = cis.readObject(); cis.close(); } else { ObjectInputStream ois = new ObjectInputStream(bis); ret = ois.readObject(); ois.close(); } return ret; } catch (IOException | ClassNotFoundException ioe) { throw new RuntimeException(ioe); } }
@Override public Object read(Kryo kryo, Input input, Class c) { int len = input.readInt(); byte[] ser = new byte[len]; input.readBytes(ser); ByteArrayInputStream bis = new ByteArrayInputStream(ser); try { ClassLoaderObjectInputStream ois = new ClassLoaderObjectInputStream(kryo.getClassLoader(), bis); return ois.readObject(); } catch (Exception e) { throw new RuntimeException(e); } } }
@org.junit.Test public void testExpected() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(baos); final Object input = Boolean.FALSE; oos.writeObject(input); final InputStream bais = new ByteArrayInputStream(baos.toByteArray()); final ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais); final Object result = clois.readObject(); assertEquals(input, result); clois.close(); }
@org.junit.Test public void testLong() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(baos); final Object input = (long) 123; oos.writeObject(input); final InputStream bais = new ByteArrayInputStream(baos.toByteArray()); final ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais); final Object result = clois.readObject(); assertEquals(input, result); clois.close(); }
@org.junit.Test public void testObject2() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(baos); final Object input = new Test(123, 0); oos.writeObject(input); oos.close(); final InputStream bais = new ByteArrayInputStream(baos.toByteArray()); final ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais); final Object result = clois.readObject(); assertEquals(input, result); clois.close(); }
@org.junit.Test public void testObject1() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(baos); final Object input = new Test(123, null); oos.writeObject(input); oos.close(); final InputStream bais = new ByteArrayInputStream(baos.toByteArray()); final ClassLoaderObjectInputStream clois = new ClassLoaderObjectInputStream(getClass().getClassLoader(), bais); final Object result = clois.readObject(); assertEquals(input, result); clois.close(); }
/** * Deserialize the input bytes into object */ @SuppressWarnings("unchecked") public static <T extends Serializable> T deserializeFromBytes(byte[] input, ClassLoader classLoader) throws Exception { //NOSONAR ByteArrayInputStream bis = new ByteArrayInputStream(input); return (T) new ClassLoaderObjectInputStream(classLoader, bis).readObject(); }
public static LogicalPlan read(InputStream is) throws IOException, ClassNotFoundException { return (LogicalPlan)new ClassLoaderObjectInputStream(Thread.currentThread().getContextClassLoader(), is).readObject(); }
@Override public Object read(InputStream is) throws IOException { byte[] bytes = (byte[]) super.read(is); if (bytes == null) { return null; } else { ClassLoaderObjectInputStream classLoaderIS = new ClassLoaderObjectInputStream(this.getClassLoader(), is); try { return classLoaderIS.readObject(); } catch (ClassNotFoundException e) { logger.warn(e.getMessage()); IOException iox = new IOException(); iox.initCause(e); throw iox; } finally { classLoaderIS.close(); } } }