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