Search in sources :

Example 66 with ObjectOutputStream

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);
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 67 with ObjectOutputStream

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;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 68 with ObjectOutputStream

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 69 with ObjectOutputStream

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 70 with ObjectOutputStream

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;
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

ObjectOutputStream (java.io.ObjectOutputStream)1102 ByteArrayOutputStream (java.io.ByteArrayOutputStream)760 ObjectInputStream (java.io.ObjectInputStream)428 ByteArrayInputStream (java.io.ByteArrayInputStream)375 IOException (java.io.IOException)358 FileOutputStream (java.io.FileOutputStream)161 Test (org.junit.Test)161 File (java.io.File)96 BufferedOutputStream (java.io.BufferedOutputStream)52 ObjectOutput (java.io.ObjectOutput)47 OutputStream (java.io.OutputStream)37 HashMap (java.util.HashMap)37 ArrayList (java.util.ArrayList)33 FileInputStream (java.io.FileInputStream)25 InputStream (java.io.InputStream)23 FileNotFoundException (java.io.FileNotFoundException)22 Serializable (java.io.Serializable)17 GZIPOutputStream (java.util.zip.GZIPOutputStream)16 Test (org.testng.annotations.Test)15 NotSerializableException (java.io.NotSerializableException)14