use of java.io.ObjectOutputStream in project Gaffer by gchq.
the class JavaSerialiser method serialise.
public byte[] serialise(final Object object) throws SerialisationException {
ObjectOutputStream out = null;
ByteArrayOutputStream byteOut = null;
try {
byteOut = new ByteArrayOutputStream();
out = new ObjectOutputStream(byteOut);
out.writeObject(object);
return byteOut.toByteArray();
} catch (final IOException e) {
throw new SerialisationException("Unable to serialise given object of class: " + object.getClass().getName() + ", does it implement the serializable interface?", e);
} finally {
close(out);
close(byteOut);
}
}
use of java.io.ObjectOutputStream in project XobotOS by xamarin.
the class AnnotationMember method rethrowError.
/**
* Throws contained error (if any) with a renewed stack trace.
*/
public void rethrowError() throws Throwable {
if (tag == ERROR) {
// first check for expected types
if (value instanceof TypeNotPresentException) {
TypeNotPresentException tnpe = (TypeNotPresentException) value;
throw new TypeNotPresentException(tnpe.typeName(), tnpe.getCause());
} else if (value instanceof EnumConstantNotPresentException) {
EnumConstantNotPresentException ecnpe = (EnumConstantNotPresentException) value;
throw new EnumConstantNotPresentException(ecnpe.enumType(), ecnpe.constantName());
} else if (value instanceof ArrayStoreException) {
ArrayStoreException ase = (ArrayStoreException) value;
throw new ArrayStoreException(ase.getMessage());
}
// got some other error, have to go with deep cloning
// via serialization mechanism
Throwable error = (Throwable) value;
StackTraceElement[] ste = error.getStackTrace();
ByteArrayOutputStream bos = new ByteArrayOutputStream(ste == null ? 512 : (ste.length + 1) * 80);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(error);
oos.flush();
oos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
error = (Throwable) ois.readObject();
ois.close();
throw error;
}
}
use of java.io.ObjectOutputStream in project hibernate-orm by hibernate.
the class NonSortedExecutableListTest method testSerializeDeserialize.
@Test
public void testSerializeDeserialize() throws IOException, ClassNotFoundException {
l.add(action4);
l.add(action3);
l.add(action2);
l.add(action1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
l.writeExternal(oos);
// this OOS stream needs to be flushed...
oos.flush();
ByteArrayInputStream bin = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bin);
l = new ExecutableList<NonSortedExecutableListTest.AnExecutable>(false);
l.readExternal(ois);
Assert.assertEquals(4, l.size());
Assert.assertEquals(action4, l.get(0));
Assert.assertEquals(action3, l.get(1));
Assert.assertEquals(action2, l.get(2));
Assert.assertEquals(action1, l.get(3));
Assert.assertFalse(l.get(0).afterDeserializeCalled);
Assert.assertFalse(l.get(1).afterDeserializeCalled);
Assert.assertFalse(l.get(2).afterDeserializeCalled);
Assert.assertFalse(l.get(3).afterDeserializeCalled);
l.afterDeserialize(null);
Assert.assertTrue(l.get(0).afterDeserializeCalled);
Assert.assertTrue(l.get(1).afterDeserializeCalled);
Assert.assertTrue(l.get(2).afterDeserializeCalled);
Assert.assertTrue(l.get(3).afterDeserializeCalled);
Assert.assertEquals(action4, l.get(0));
Assert.assertEquals(action3, l.get(1));
Assert.assertEquals(action2, l.get(2));
Assert.assertEquals(action1, l.get(3));
// sort after deserializing; it should still have no affect
l.sort();
Assert.assertEquals(action4, l.get(0));
Assert.assertEquals(action3, l.get(1));
Assert.assertEquals(action2, l.get(2));
Assert.assertEquals(action1, l.get(3));
}
use of java.io.ObjectOutputStream in project hibernate-orm by hibernate.
the class SortedExecutableListTest method testSerializeDeserialize.
@Test
public void testSerializeDeserialize() throws IOException, ClassNotFoundException {
l.add(action4);
l.add(action3);
l.add(action2);
l.add(action1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
l.writeExternal(oos);
// this OOS stream needs to be flushed...
oos.flush();
ByteArrayInputStream bin = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bin);
l = new ExecutableList<SortedExecutableListTest.AnExecutable>();
l.readExternal(ois);
Assert.assertEquals(4, l.size());
Assert.assertEquals(action4, l.get(0));
Assert.assertEquals(action3, l.get(1));
Assert.assertEquals(action2, l.get(2));
Assert.assertEquals(action1, l.get(3));
Assert.assertFalse(l.get(0).afterDeserializeCalled);
Assert.assertFalse(l.get(1).afterDeserializeCalled);
Assert.assertFalse(l.get(2).afterDeserializeCalled);
Assert.assertFalse(l.get(3).afterDeserializeCalled);
l.afterDeserialize(null);
Assert.assertTrue(l.get(0).afterDeserializeCalled);
Assert.assertTrue(l.get(1).afterDeserializeCalled);
Assert.assertTrue(l.get(2).afterDeserializeCalled);
Assert.assertTrue(l.get(3).afterDeserializeCalled);
Assert.assertEquals(action4, l.get(0));
Assert.assertEquals(action3, l.get(1));
Assert.assertEquals(action2, l.get(2));
Assert.assertEquals(action1, l.get(3));
// sort after deserializing
l.sort();
Assert.assertEquals(action1, l.get(0));
Assert.assertEquals(action2, l.get(1));
Assert.assertEquals(action3, l.get(2));
Assert.assertEquals(action4, l.get(3));
}
use of java.io.ObjectOutputStream in project hibernate-orm by hibernate.
the class DeleteOrphanTest method serialize.
private byte[] serialize(Object object) throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(stream);
out.writeObject(object);
out.close();
byte[] serialized = stream.toByteArray();
stream.close();
return serialized;
}
Aggregations