Search in sources :

Example 56 with ObjectOutputStream

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) EqualsTester(com.google.common.testing.EqualsTester) InMemoryFileSystem(com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 57 with ObjectOutputStream

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 58 with ObjectOutputStream

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);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 59 with ObjectOutputStream

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 60 with ObjectOutputStream

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();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

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