use of java.io.ObjectOutputStream in project bazel by bazelbuild.
the class PathTest method testSerialization.
@Test
public void testSerialization() throws Exception {
FileSystem oldFileSystem = Path.getFileSystemForSerialization();
try {
Path.setFileSystemForSerialization(filesystem);
Path root = filesystem.getPath("/");
Path p1 = filesystem.getPath("/foo");
Path p2 = filesystem.getPath("/foo/bar");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(root);
oos.writeObject(p1);
oos.writeObject(p2);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Path dsRoot = (Path) ois.readObject();
Path dsP1 = (Path) ois.readObject();
Path dsP2 = (Path) ois.readObject();
new EqualsTester().addEqualityGroup(root, dsRoot).addEqualityGroup(p1, dsP1).addEqualityGroup(p2, dsP2).testEquals();
assertTrue(p2.startsWith(p1));
assertTrue(p2.startsWith(dsP1));
assertTrue(dsP2.startsWith(p1));
assertTrue(dsP2.startsWith(dsP1));
// Regression test for a very specific bug in compareTo involving our incorrect usage of
// reference equality rather than logical equality.
String relativePathStringA = "child/grandchildA";
String relativePathStringB = "child/grandchildB";
assertEquals(p1.getRelative(relativePathStringA).compareTo(p1.getRelative(relativePathStringB)), p1.getRelative(relativePathStringA).compareTo(dsP1.getRelative(relativePathStringB)));
} finally {
Path.setFileSystemForSerialization(oldFileSystem);
}
}
use of java.io.ObjectOutputStream in project bazel by bazelbuild.
the class PackageIdentifierTest method testSerialization.
@Test
public void testSerialization() throws Exception {
PackageIdentifier inId = PackageIdentifier.create("@foo", new PathFragment("bar/baz"));
ByteArrayOutputStream data = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(data);
out.writeObject(inId);
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data.toByteArray()));
PackageIdentifier outId = (PackageIdentifier) in.readObject();
assertEquals(inId, outId);
}
use of java.io.ObjectOutputStream in project camel by apache.
the class SerializationUtils method marshal.
public static byte[] marshal(Object o) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(out)) {
oout.writeObject(o);
oout.flush();
return out.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Unable to marshal the bean " + o, e);
}
}
use of java.io.ObjectOutputStream in project gitblit by gitblit.
the class TicketModel method copy.
/**
* Produce a deep copy of the given object. Serializes the entire object to
* a byte array in memory. Recommended for relatively small objects.
*/
@SuppressWarnings("unchecked")
static <T> T copy(T original) {
T o = null;
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(byteOut);
oos.writeObject(original);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream ois = new ObjectInputStream(byteIn);
try {
o = (T) ois.readObject();
} catch (ClassNotFoundException cex) {
// actually can not happen in this instance
}
} catch (IOException iox) {
// doesn't seem likely to happen as these streams are in memory
throw new RuntimeException(iox);
}
return o;
}
use of java.io.ObjectOutputStream in project gephi by gephi.
the class RecentPalettes method serializePositions.
private byte[] serializePositions(float[] positions) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(positions);
out.close();
return bos.toByteArray();
}
Aggregations