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));
}
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");
}
}
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());
}
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");
}
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) {
}
}
}
Aggregations