Example usage for java.io ObjectOutputStream ObjectOutputStream

List of usage examples for java.io ObjectOutputStream ObjectOutputStream

Introduction

In this page you can find the example usage for java.io ObjectOutputStream ObjectOutputStream.

Prototype

public ObjectOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates an ObjectOutputStream that writes to the specified OutputStream.

Usage

From source file:org.ambraproject.model.article.ArticleTypeTest.java

@Test
public void testDeserializedArticleTypeEquality() throws Exception {
    ArticleType art1 = ArticleType// w  w w  .jav  a 2 s . co m
            .getArticleTypeForURI(new URI("http://rdf.plos.org/RDF/articleType/Interview"), false);

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bout);

    out.writeObject(art1);
    out.flush();

    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    ObjectInputStream in = new ObjectInputStream(bin);

    ArticleType art2 = (ArticleType) in.readObject();

    assertTrue(art1 == art2, "Article 1 == Article 2");
    assertTrue(art1.equals(art2), "Article 1 should .equals() Article 2");
}

From source file:jfs.sync.encryption.JFSEncryptedStream.java

public static OutputStream createOutputStream(long compressionLimit, OutputStream baseOutputStream, long length,
        Cipher cipher) throws IOException {
    OutputStream result = null;//from   w  w  w  .j  av a  2  s  .c om
    Runtime runtime = Runtime.getRuntime();
    long freeMem = runtime.totalMemory() / SPACE_RESERVE;
    if (length >= freeMem) {
        if (log.isWarnEnabled()) {
            log.warn("JFSEncryptedStream.createOutputStream() GC " + freeMem + "/" + runtime.maxMemory());
        } // if
        runtime.gc();
        freeMem = runtime.totalMemory() / SPACE_RESERVE;
    } // if
    if ((length < compressionLimit) && (length < freeMem)) {
        result = new JFSEncryptedStream(baseOutputStream, cipher);
    } else {
        if (length < freeMem) {
            if (log.isInfoEnabled()) {
                log.info("JFSEncryptedStream.createOutputStream() not compressing");
            } // if
        } else {
            if (log.isWarnEnabled()) {
                log.warn("JFSEncryptedStream.createOutputStream() due to memory constraints (" + length + "/"
                        + freeMem + ") not compressing");
            } // if
        } // if
        ObjectOutputStream oos = new ObjectOutputStream(baseOutputStream);
        oos.writeByte(COMPRESSION_NONE);
        oos.writeLong(length);
        oos.flush();
        result = baseOutputStream;
        if (cipher != null) {
            result = new CipherOutputStream(result, cipher);
        } // if
    } // if
    return result;
}

From source file:de.tudarmstadt.ukp.dkpro.wsd.si.dictionary.GoogleDictionaryInventory.java

public GoogleDictionaryInventory(String inputPath, String serializiblePath, String neededMentionsPath)
        throws FileNotFoundException, IOException {
    // Open the serialized version or create it from scratch
    try {//w  w w.  ja v  a2s . c  o m
        logger.debug("Trying to load dictionary " + this.getClass().getSimpleName() + " from serializable.");
        ObjectInputStream dictionaryReader = new ObjectInputStream(
                new BZip2CompressorInputStream(new FileInputStream(serializiblePath)));
        dictionary = (GoogleDictionary) dictionaryReader.readObject();
        dictionaryReader.close();
        logger.debug("Loaded dictionary " + this.getClass().getSimpleName() + " from serializable.");
    } catch (Exception e) {
        logger.debug("Trying to load dictionary " + this.getClass().getSimpleName() + " from input.");
        dictionary = new GoogleDictionary(inputPath, neededMentionsPath);
        System.out.println("Loaded dictionary " + this.getClass().getSimpleName() + " from input.");

        ObjectOutputStream dictionaryWriter = new ObjectOutputStream(
                new BZip2CompressorOutputStream(new FileOutputStream(serializiblePath)));
        dictionaryWriter.writeObject(dictionary);
        dictionaryWriter.close();
        logger.debug("Stored dictionary " + this.getClass().getSimpleName() + " in serializable.");

    }
}

From source file:com.sec.ose.osi.sdk.protexsdk.component.ComponentAPIWrapper.java

public static void save() {
    ObjectOutputStream oos;//from   w  w  w  .jav a 2s  .c  o  m
    try {
        oos = new ObjectOutputStream(new FileOutputStream(new File(COMPONENT_FILE_PATH)));
        for (ComponentInfo tmp : globalComponentManager.getComponentList()) {
            oos.writeObject(tmp);
        }
        oos.flush();
        oos.close();
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}

From source file:edu.harvard.i2b2.loinc.BinResourceFromLoincData.java

public void serializeLoincCodeToNameMap() throws IOException {

    FileOutputStream fos = null;//  w w w .ja va 2s. c  o m
    ObjectOutputStream oos = null;

    try {
        fos = new FileOutputStream("loincCodeToNameMap.bin");

        oos = new ObjectOutputStream(fos);
        oos.writeObject(loincCodeToNameMap);

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        fos.close();
        oos.close();
    }
}

From source file:com.zaubersoftware.gnip4j.api.impl.XMLActivityStreamFeedProcessorTest.java

/** test */
@Test/* ww  w.j  a va  2 s .c  o m*/
public final void test() throws IOException, ParseException {
    final InputStream is = getClass().getResourceAsStream("fanpage.xml");
    try {
        final AtomicInteger i = new AtomicInteger();
        final ObjectMapper mapper = JsonActivityFeedProcessor.getObjectMapper();
        final FeedProcessor p = new XMLActivityStreamFeedProcessor<Activity>("foo", new DirectExecuteService(),
                new StreamNotificationAdapter<Activity>() {
                    @Override
                    public void notify(final Activity activity, final GnipStream stream) {
                        i.incrementAndGet();
                        try {
                            final byte[] data0 = mapper.writeValueAsBytes(activity);
                            final Activity e = mapper.reader(Activity.class).readValue(data0);
                            final byte[] data1 = mapper.writeValueAsBytes(e);
                            assertArrayEquals(data0, data1);

                            // test serialization
                            final ObjectOutputStream os = new ObjectOutputStream(new ByteArrayOutputStream());
                            os.writeObject(activity);
                            os.close();
                        } catch (final Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                }, new ActivityUnmarshaller("hola"));
        p.process(is);
        assertEquals(23, i.get());
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.openteach.diamond.network.waverider.master.MasterState.java

public ByteBuffer toByteBuffer() {
    ByteArrayOutputStream bout = null;
    ObjectOutputStream oout = null;
    try {/*from w  w  w. j a  v a2  s.c o m*/
        bout = new ByteArrayOutputStream();
        oout = new ObjectOutputStream(bout);
        oout.writeObject(this);
        oout.flush();
        return ByteBuffer.wrap(bout.toByteArray());
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (oout != null) {
                oout.close();
            }
            if (bout != null) {
                bout.close();
            }
        } catch (IOException e) {
            logger.error(e);
        }
    }
}

From source file:net.schweerelos.parrot.ui.NodeWrapperPersistentLayoutImpl.java

@Override
public synchronized void persist(String fileName) throws IOException {
    uriToNodeLocation.clear();//from  www . j  a  v a 2  s.  c o  m

    for (NodeWrapper vertex : getGraph().getVertices()) {
        Point p = new Point(transform(vertex));
        uriToNodeLocation.put(vertex.getOntResource().getURI(), p);
    }

    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
    oos.writeObject(uriToNodeLocation);
    oos.close();
}

From source file:com.discovery.darchrow.io.SerializableUtil.java

/**
 * To byte array output stream./*ww  w. jav  a  2 s.  co  m*/
 *
 * @param serializable
 *            the serializable
 * @return the byte array output stream
 * @see java.io.ObjectOutputStream#ObjectOutputStream(OutputStream)
 * @see java.io.ObjectOutputStream#writeObject(Object)
 * @see org.apache.commons.lang3.SerializationUtils#serialize(Serializable, OutputStream)
 */
private static ByteArrayOutputStream toByteArrayOutputStream(Serializable serializable) {
    ObjectOutputStream objectOutputStream = null;
    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(serializable);

        return byteArrayOutputStream;
    } catch (IOException e) {
        LOGGER.error("", e);
        throw new SerializationException(e);
    } finally {
        IOUtils.closeQuietly(objectOutputStream);
    }
}

From source file:com.adito.boot.AbstractPropertyClass.java

public void store() throws IOException {
    if (store != null) {
        throw new IllegalStateException("Already storing property class. Either restor or reset first.");
    }/*w  ww  .  j  ava 2  s .  c om*/
    store = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(store);
    try {
        oos.writeObject(definitions);
        oos.writeObject(categories);
        oos.writeObject(categoryMap);
    } finally {
        oos.close();
    }
}