use of java.io.ObjectStreamClass in project camel by apache.
the class HBaseHelper method fromBytes.
public Object fromBytes(byte[] binary) {
Object result = null;
ObjectInputStream ois = null;
if (binary == null) {
return null;
}
ByteArrayInputStream bais = new ByteArrayInputStream(binary);
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
ois = new ObjectInputStream(bais) {
@Override
public Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
try {
return classLoader.loadClass(desc.getName());
} catch (Exception e) {
}
return super.resolveClass(desc);
}
};
result = ois.readObject();
} catch (IOException e) {
LOG.warn("Error while deserializing object. Null will be used.", e);
} catch (ClassNotFoundException e) {
LOG.warn("Could not find class while deserializing object. Null will be used.", e);
} finally {
IOHelper.close(ois);
IOHelper.close(bais);
}
return result;
}
use of java.io.ObjectStreamClass in project camel by apache.
the class KratiDefaultSerializer method deserialize.
/**
* Deserialize an object from its raw bytes generated by {{@link #serialize(Object)}.
*
* @param binary - the raw bytes from which an object is constructed.
* @return an object constructed from the raw bytes.
* @throws SerializationException if the object cannot be constructed from the raw bytes.
*/
@SuppressWarnings("unchecked")
public T deserialize(byte[] binary) throws SerializationException {
T result = null;
ObjectInputStream ois = null;
if (binary == null) {
return null;
}
// TODO: should use Camel's ClassResolver for classloading
ByteArrayInputStream bais = new ByteArrayInputStream(binary);
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
ois = new ObjectInputStream(bais) {
@Override
public Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
try {
return classLoader.loadClass(desc.getName());
} catch (Exception e) {
}
return super.resolveClass(desc);
}
};
result = (T) ois.readObject();
} catch (IOException e) {
LOG.warn("Error while deserializing object. Null will be used.", e);
} catch (ClassNotFoundException e) {
LOG.warn("Could not find class while deserializing object. Null will be used.", e);
} finally {
IOHelper.close(ois, bais);
}
return result;
}
use of java.io.ObjectStreamClass in project robovm by robovm.
the class SerializationTest method testSerializeFieldMadeTransient.
// http://b/4471249
public void testSerializeFieldMadeTransient() throws Exception {
// Does ObjectStreamClass have the right idea?
ObjectStreamClass osc = ObjectStreamClass.lookup(FieldMadeTransient.class);
ObjectStreamField[] fields = osc.getFields();
assertEquals(1, fields.length);
assertEquals("nonTransientInt", fields[0].getName());
assertEquals(int.class, fields[0].getType());
// this was created by serializing a FieldMadeTransient with a non-0 transientInt
String s = "aced0005737200346c6962636f72652e6a6176612e696f2e53657269616c697a6174696f6e54657" + "374244669656c644d6164655472616e7369656e74000000000000000002000149000c7472616e736" + "9656e74496e747870abababab";
FieldMadeTransient deserialized = (FieldMadeTransient) SerializationTester.deserializeHex(s);
assertEquals(0, deserialized.transientInt);
}
use of java.io.ObjectStreamClass in project platform_frameworks_base by android.
the class Parcel method readSerializable.
private final Serializable readSerializable(final ClassLoader loader) {
String name = readString();
if (name == null) {
// return null, which indicates that the name wasn't found in the parcel.
return null;
}
byte[] serializedData = createByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
try {
ObjectInputStream ois = new ObjectInputStream(bais) {
@Override
protected Class<?> resolveClass(ObjectStreamClass osClass) throws IOException, ClassNotFoundException {
// try the custom classloader if provided
if (loader != null) {
Class<?> c = Class.forName(osClass.getName(), false, loader);
if (c != null) {
return c;
}
}
return super.resolveClass(osClass);
}
};
return (Serializable) ois.readObject();
} catch (IOException ioe) {
throw new RuntimeException("Parcelable encountered " + "IOException reading a Serializable object (name = " + name + ")", ioe);
} catch (ClassNotFoundException cnfe) {
throw new RuntimeException("Parcelable encountered " + "ClassNotFoundException reading a Serializable object (name = " + name + ")", cnfe);
}
}
use of java.io.ObjectStreamClass in project android_frameworks_base by AOSPA.
the class Parcel method readSerializable.
private final Serializable readSerializable(final ClassLoader loader) {
String name = readString();
if (name == null) {
// return null, which indicates that the name wasn't found in the parcel.
return null;
}
byte[] serializedData = createByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
try {
ObjectInputStream ois = new ObjectInputStream(bais) {
@Override
protected Class<?> resolveClass(ObjectStreamClass osClass) throws IOException, ClassNotFoundException {
// try the custom classloader if provided
if (loader != null) {
Class<?> c = Class.forName(osClass.getName(), false, loader);
if (c != null) {
return c;
}
}
return super.resolveClass(osClass);
}
};
return (Serializable) ois.readObject();
} catch (IOException ioe) {
throw new RuntimeException("Parcelable encountered " + "IOException reading a Serializable object (name = " + name + ")", ioe);
} catch (ClassNotFoundException cnfe) {
throw new RuntimeException("Parcelable encountered " + "ClassNotFoundException reading a Serializable object (name = " + name + ")", cnfe);
}
}
Aggregations