Search in sources :

Example 71 with ObjectInputStream

use of java.io.ObjectInputStream 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 72 with ObjectInputStream

use of java.io.ObjectInputStream 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 73 with ObjectInputStream

use of java.io.ObjectInputStream in project guava by google.

the class SetsTest method testImmutableEnumSet_deserializationMakesDefensiveCopy.

// java serialization not supported in GWT.
@GwtIncompatible
public void testImmutableEnumSet_deserializationMakesDefensiveCopy() throws Exception {
    ImmutableSet<SomeEnum> original = Sets.immutableEnumSet(SomeEnum.A, SomeEnum.B);
    int handleOffset = 6;
    byte[] serializedForm = serializeWithBackReference(original, handleOffset);
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serializedForm));
    ImmutableSet<?> deserialized = (ImmutableSet<?>) in.readObject();
    EnumSet<?> delegate = (EnumSet<?>) in.readObject();
    assertEquals(original, deserialized);
    assertTrue(delegate.remove(SomeEnum.A));
    assertTrue(deserialized.contains(SomeEnum.A));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Sets.newEnumSet(com.google.common.collect.Sets.newEnumSet) EnumSet(java.util.EnumSet) ObjectInputStream(java.io.ObjectInputStream) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 74 with ObjectInputStream

use of java.io.ObjectInputStream in project guava by google.

the class MultimapBuilderTest method reserialize.

// serialization
@GwtIncompatible
private static Object reserialize(Object object) throws Exception {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    new ObjectOutputStream(bytes).writeObject(object);
    return new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())).readObject();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 75 with ObjectInputStream

use of java.io.ObjectInputStream in project guice by google.

the class MultibinderTest method testMultibinderSetIsSerializable.

public void testMultibinderSetIsSerializable() throws IOException, ClassNotFoundException {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            Multibinder.newSetBinder(binder(), String.class).addBinding().toInstance("A");
        }
    });
    Set<String> set = injector.getInstance(Key.get(setOfString));
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteStream);
    try {
        objectOutputStream.writeObject(set);
    } finally {
        objectOutputStream.close();
    }
    ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(byteStream.toByteArray()));
    try {
        Object setCopy = objectInputStream.readObject();
        assertEquals(set, setCopy);
    } finally {
        objectInputStream.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Injector(com.google.inject.Injector) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) AbstractModule(com.google.inject.AbstractModule) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ObjectInputStream (java.io.ObjectInputStream)1041 ByteArrayInputStream (java.io.ByteArrayInputStream)667 ObjectOutputStream (java.io.ObjectOutputStream)427 ByteArrayOutputStream (java.io.ByteArrayOutputStream)354 IOException (java.io.IOException)341 FileInputStream (java.io.FileInputStream)152 Test (org.junit.Test)128 File (java.io.File)89 InputStream (java.io.InputStream)85 BufferedInputStream (java.io.BufferedInputStream)47 Serializable (java.io.Serializable)40 HashMap (java.util.HashMap)35 ArrayList (java.util.ArrayList)31 FileNotFoundException (java.io.FileNotFoundException)27 FileOutputStream (java.io.FileOutputStream)27 Test (org.testng.annotations.Test)26 Map (java.util.Map)25 EOFException (java.io.EOFException)21 GZIPInputStream (java.util.zip.GZIPInputStream)21 ObjectInput (java.io.ObjectInput)20