Search in sources :

Example 76 with ObjectOutputStream

use of java.io.ObjectOutputStream in project hibernate-orm by hibernate.

the class GetterSetterSerializationTest method testPrivateFieldGetter.

@Test
@TestForIssue(jiraKey = "HHH-11202")
public void testPrivateFieldGetter() throws Exception {
    final AnEntity entity = new AnEntity(new PK(1L));
    final Getter getter = new GetterFieldImpl(AnEntity.class, "pk", ReflectHelper.findField(AnEntity.class, "pk"));
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(getter);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Getter getterClone = (Getter) ois.readObject();
    assertSame(getter.get(entity), getterClone.get(entity));
}
Also used : GetterFieldImpl(org.hibernate.property.access.spi.GetterFieldImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) Getter(org.hibernate.property.access.spi.Getter) PK(org.hibernate.serialization.entity.PK) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnEntity(org.hibernate.serialization.entity.AnEntity) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 77 with ObjectOutputStream

use of java.io.ObjectOutputStream in project hibernate-orm by hibernate.

the class FumTest method spoofSerialization.

private Session spoofSerialization(Session session) throws IOException {
    try {
        // Serialize the incoming out to memory
        ByteArrayOutputStream serBaOut = new ByteArrayOutputStream();
        ObjectOutputStream serOut = new ObjectOutputStream(serBaOut);
        serOut.writeObject(session);
        // Now, re-constitute the model from memory
        ByteArrayInputStream serBaIn = new ByteArrayInputStream(serBaOut.toByteArray());
        ObjectInputStream serIn = new ObjectInputStream(serBaIn);
        Session outgoing = (Session) serIn.readObject();
        return outgoing;
    } catch (ClassNotFoundException cnfe) {
        throw new IOException("Unable to locate class on reconstruction");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Session(org.hibernate.Session)

Example 78 with ObjectOutputStream

use of java.io.ObjectOutputStream in project hazelcast by hazelcast.

the class SerializationTest method testMemberLeftException.

private void testMemberLeftException(String uuid, String host, int port, Member member) throws Exception {
    MemberLeftException exception = new MemberLeftException(member);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bout);
    out.writeObject(exception);
    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    ObjectInputStream in = new ObjectInputStream(bin);
    MemberLeftException exception2 = (MemberLeftException) in.readObject();
    MemberImpl member2 = (MemberImpl) exception2.getMember();
    assertEquals(uuid, member2.getUuid());
    assertEquals(host, member2.getAddress().getHost());
    assertEquals(port, member2.getAddress().getPort());
    assertEquals(member.isLiteMember(), member2.isLiteMember());
    assertEquals(member.getVersion(), member2.getVersion());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleMemberImpl(com.hazelcast.instance.SimpleMemberImpl) MemberImpl(com.hazelcast.instance.MemberImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) MemberLeftException(com.hazelcast.core.MemberLeftException) ObjectInputStream(java.io.ObjectInputStream)

Example 79 with ObjectOutputStream

use of java.io.ObjectOutputStream in project guava by hceylan.

the class ReserializingTestCollectionGenerator method reserialize.

@SuppressWarnings("unchecked")
static <T> T reserialize(T object) {
    try {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bytes);
        out.writeObject(object);
        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
        return (T) in.readObject();
    } catch (IOException e) {
        Helpers.fail(e, e.getMessage());
    } catch (ClassNotFoundException e) {
        Helpers.fail(e, e.getMessage());
    }
    throw new AssertionError("not reachable");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 80 with ObjectOutputStream

use of java.io.ObjectOutputStream in project hibernate-orm by hibernate.

the class SerializationHelper method serialize.

// Serialize
//-----------------------------------------------------------------------
/**
	 * <p>Serializes an <code>Object</code> to the specified stream.</p>
	 * <p/>
	 * <p>The stream will be closed once the object is written.
	 * This avoids the need for a finally clause, and maybe also exception
	 * handling, in the application code.</p>
	 * <p/>
	 * <p>The stream passed in is not buffered internally within this method.
	 * This is the responsibility of your application if desired.</p>
	 *
	 * @param obj the object to serialize to bytes, may be null
	 * @param outputStream the stream to write to, must not be null
	 *
	 * @throws IllegalArgumentException if <code>outputStream</code> is <code>null</code>
	 * @throws SerializationException (runtime) if the serialization fails
	 */
public static void serialize(Serializable obj, OutputStream outputStream) throws SerializationException {
    if (outputStream == null) {
        throw new IllegalArgumentException("The OutputStream must not be null");
    }
    if (LOG.isTraceEnabled()) {
        if (Hibernate.isInitialized(obj)) {
            LOG.tracev("Starting serialization of object [{0}]", obj);
        } else {
            LOG.trace("Starting serialization of [uninitialized proxy]");
        }
    }
    ObjectOutputStream out = null;
    try {
        // stream closed in the finally
        out = new ObjectOutputStream(outputStream);
        out.writeObject(obj);
    } catch (IOException ex) {
        throw new SerializationException("could not serialize", ex);
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException ignored) {
        }
    }
}
Also used : SerializationException(org.hibernate.type.SerializationException) IOException(java.io.IOException) 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