Search in sources :

Example 51 with ObjectInputStream

use of java.io.ObjectInputStream in project hazelcast by hazelcast.

the class MulticastDiscoveryReceiver method receive.

public MulticastMemberInfo receive() {
    ObjectInputStream in = null;
    ByteArrayInputStream bis = null;
    try {
        Object o;
        multicastSocket.receive(datagramPacketReceive);
        byte[] data = datagramPacketReceive.getData();
        MulticastMemberInfo multicastMemberInfo;
        bis = new ByteArrayInputStream(data);
        in = new ObjectInputStream(bis);
        o = in.readObject();
        multicastMemberInfo = (MulticastMemberInfo) o;
        return multicastMemberInfo;
    } catch (Exception e) {
        if (logger.isFinestEnabled()) {
            logger.finest("Couldn't get member info from multicast channel " + e.getMessage());
        }
    } finally {
        IOUtil.closeResource(bis);
        IOUtil.closeResource(in);
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 52 with ObjectInputStream

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

use of java.io.ObjectInputStream in project CoreNLP by stanfordnlp.

the class Extractors method main.

/**
   * Prints out the pair of {@code Extractors} objects found in the
   * file that is the first and only argument.
   * @param args Filename of extractors file (standardly written with
   *       {@code .ex} extension)
   */
public static void main(String[] args) {
    try {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(args[0]));
        Extractors extrs = (Extractors) in.readObject();
        Extractors extrsRare = (Extractors) in.readObject();
        in.close();
        System.out.println("All words:  " + extrs);
        System.out.println("Rare words: " + extrsRare);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 54 with ObjectInputStream

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

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

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