Search in sources :

Example 36 with ObjectOutputStream

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

the class JSR166TestCase method serialClone.

@SuppressWarnings("unchecked")
<T> T serialClone(T o) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(o);
        oos.flush();
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
        T clone = (T) ois.readObject();
        assertSame(o.getClass(), clone.getClass());
        return clone;
    } catch (Throwable t) {
        threadUnexpectedException(t);
        return null;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 37 with ObjectOutputStream

use of java.io.ObjectOutputStream in project j2objc by google.

the class MyObjectInputStream method test_getType_Deserialized.

/**
     * java.io.ObjectStreamField#getType()
     */
public void test_getType_Deserialized() throws IOException, ClassNotFoundException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(new SerializableObject());
    oos.close();
    baos.close();
    byte[] bytes = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bais);
    SerializableObject obj = (SerializableObject) ois.readObject();
    ObjectStreamClass oc = obj.getObjectStreamClass();
    ObjectStreamField field = oc.getField("i");
    assertEquals(Object.class, field.getType());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectStreamField(java.io.ObjectStreamField) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectStreamClass(java.io.ObjectStreamClass) ObjectInputStream(java.io.ObjectInputStream)

Example 38 with ObjectOutputStream

use of java.io.ObjectOutputStream in project j2objc by google.

the class ObjectOutputStreamTest method testLongString.

public void testLongString() throws Exception {
    // Most modified UTF-8 is limited to 64KiB, but serialized strings can have an 8-byte
    // length, so this should never throw java.io.UTFDataFormatException...
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 64 * 1024 * 2; ++i) {
        sb.append('a');
    }
    String s = sb.toString();
    ObjectOutputStream os = new ObjectOutputStream(new ByteArrayOutputStream());
    os.writeObject(s);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 39 with ObjectOutputStream

use of java.io.ObjectOutputStream in project j2objc by google.

the class SerializationTest method testSerializingNotSerializableClass.

// Regression test for https://github.com/google/j2objc/issues/496.
@SuppressWarnings("resource")
public void testSerializingNotSerializableClass() throws Exception {
    File tmpFile = File.createTempFile("filec", "object");
    try {
        FileOutputStream fileOut = new FileOutputStream(tmpFile);
        new ObjectOutputStream(fileOut).writeObject(NotSerializableClass.class);
        FileInputStream fileIn = new FileInputStream(tmpFile);
        assertEquals(NotSerializableClass.class, new ObjectInputStream(fileIn).readObject());
    } finally {
        tmpFile.delete();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 40 with ObjectOutputStream

use of java.io.ObjectOutputStream in project j2objc by google.

the class SerializationTest method testArraySerialization.

public void testArraySerialization() throws Exception {
    String[] names = new String[] { "tom", "dick", "harry" };
    assertTrue("array is not Serializable", names instanceof Serializable);
    assertTrue("array is not instance of Serializable", Serializable.class.isInstance(names));
    assertTrue("array cannot be assigned to Serializable", Serializable.class.isAssignableFrom(names.getClass()));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(out);
    oos.writeObject(names);
    oos.close();
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(in);
    String[] result = (String[]) ois.readObject();
    ois.close();
    assertTrue("arrays not equal", Arrays.equals(names, result));
}
Also used : Serializable(java.io.Serializable) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

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