下面列出了怎么用org.apache.commons.io.input.ClassLoaderObjectInputStream的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}
}
public UserEntity deserializeObjectWithInheritance(InputStream receivedFile) throws IOException, ClassNotFoundException {
ClassLoaderObjectInputStream in = new ClassLoaderObjectInputStream(getClass().getClassLoader(), receivedFile);
try {
return (UserEntity) in.readObject();
}
finally {
in.close();
}
}
/**
* deserialize byte array as Object.
*
* <p></p>
* BORROwED FROM APACHE STORM PROJECT
*
* @param serialized byte[]
* @return Object
*/
public static Object deserialize(byte[] serialized) {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
ObjectInputStream ois = new ClassLoaderObjectInputStream(classLoader, bis);
Object ret = ois.readObject();
ois.close();
return ret;
} catch (IOException | ClassNotFoundException ioe) {
throw new RuntimeException(ioe);
}
}
static <T extends VoidMessage> T fromBytes(byte[] array) {
try {
ObjectInputStream in = new ClassLoaderObjectInputStream(Thread.currentThread().getContextClassLoader(),
new ByteArrayInputStream(array));
T result = (T) in.readObject();
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
//return SerializationUtils.deserialize(array);
}
static <T extends VoidMessage> T fromBytes(byte[] array) {
try {
ObjectInputStream in = new ClassLoaderObjectInputStream(Thread.currentThread().getContextClassLoader(),
new ByteArrayInputStream(array));
T result = (T) in.readObject();
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
//return SerializationUtils.deserialize(array);
}
@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);
}
}
public static LogicalPlan read(InputStream is) throws IOException, ClassNotFoundException
{
return (LogicalPlan)new ClassLoaderObjectInputStream(Thread.currentThread().getContextClassLoader(), is).readObject();
}
public static LogicalPlan read(InputStream is) throws IOException, ClassNotFoundException
{
return (LogicalPlan)new ClassLoaderObjectInputStream(Thread.currentThread().getContextClassLoader(), is).readObject();
}