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.eclipse.thym.core.internal.util.BundleHttpCacheStorage.java

@Override
public void putEntry(String key, HttpCacheEntry entry) throws IOException {
    ByteArrayOutputStream byteArrayOS = null;
    ObjectOutputStream objectOut = null;

    try {//  w w w  .j  a va  2 s .  c o m
        File f = getCacheFile(key);
        byteArrayOS = new ByteArrayOutputStream();
        objectOut = new ObjectOutputStream(byteArrayOS);
        objectOut.writeObject(entry);
        objectOut.flush();
        FileUtils.writeByteArrayToFile(f, byteArrayOS.toByteArray());
    } finally {
        if (objectOut != null)
            objectOut.close();
        if (byteArrayOS != null)
            byteArrayOS.close();
    }
}

From source file:de.zib.gndms.gritserv.tests.EncTest.java

public static void dryRun(EndpointReferenceType epr) throws Exception {

    ByteArrayOutputStream bse = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bse);
    String sepr = ObjectSerializer.toString(epr, QNAME);
    System.out.println(sepr);//from ww  w.  ja  va2 s  . co  m
    oos.writeObject(sepr);

    Base64 b64 = new Base64(0, new byte[] {}, true);
    String uuepr = b64.encodeToString(bse.toByteArray());
    System.out.println("uuepr: \"" + uuepr + "\"");

    byte[] bt = b64.decode(uuepr.getBytes());

    ByteArrayInputStream bis = new ByteArrayInputStream(bt);
    ObjectInputStream ois = new ObjectInputStream(bis);
    String eprs = (String) ois.readObject();
    System.out.println(eprs);
}

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

@Test
public void testGoogleDictionary() throws Exception {

    FileUtils.deleteQuietly(new File(output));

    GoogleDictionary dictionary = new GoogleDictionary(path, needed_mentions);
    Assert.assertNotNull(dictionary);/* www  .j  a  v  a 2  s  .  c  om*/

    ObjectOutputStream dictionaryWriter = new ObjectOutputStream(
            new BZip2CompressorOutputStream(new FileOutputStream(output)));
    dictionaryWriter.writeObject(dictionary);
    dictionaryWriter.close();

    Assert.assertTrue(new File(output).exists());

    ObjectInputStream dictionaryReader = new ObjectInputStream(
            new BZip2CompressorInputStream(new FileInputStream(output)));

    dictionary = null;
    dictionary = (GoogleDictionary) dictionaryReader.readObject();

    Assert.assertNotNull(dictionary);
    System.out.println(dictionary.getNumberOfMentionEntityPairs());
    System.out.println(dictionary.getTargetSize());
    Assert.assertEquals(3, dictionary.getTargetValuePairs("claude_monet").size());

    dictionaryReader.close();

}

From source file:com.thoughtworks.acceptance.WriteReplaceTest.java

public void testReplacesAndResolves() throws IOException, ClassNotFoundException {
    Thing thing = new Thing(3, 6);

    // ensure that Java serialization does not cause endless loop for a Thing
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(thing);//from   ww w. j  a v a2 s .  c  o m
    oos.close();

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ios = new ObjectInputStream(bais);
    assertEquals(thing, ios.readObject());
    ios.close();

    // ensure that XStream does not cause endless loop for a Thing
    xstream.alias("thing", Thing.class);

    String expectedXml = "" + "<thing>\n" + "  <a>3000</a>\n" + "  <b>6000</b>\n" + "</thing>";

    assertBothWays(thing, expectedXml);
}

From source file:de.tudarmstadt.ukp.csniper.resbuild.BinaryCasWriter.java

@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {

    OutputStream docOS = null;//  w  w  w  .  ja va2 s  . com
    try {
        w1.resume();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(500 * 1024);
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        CASCompleteSerializer serializer = new CASCompleteSerializer(aJCas.getCasImpl());
        oos.writeObject(serializer);
        closeQuietly(oos);
        size1 += bos.size();
        w1.suspend();

        w3.resume();
        ByteArrayOutputStream bo2s = new ByteArrayOutputStream(500 * 1024);
        XmiCasSerializer.serialize(aJCas.getCas(), bo2s);
        size2 += bo2s.size();
        w3.suspend();

        //          w2.resume();
        //            docOS = getOutputStream(aJCas, ".bin");
        //            docOS.write(bos.toByteArray());
        //          w2.suspend();
    } catch (Exception e) {
        throw new AnalysisEngineProcessException(e);
    } finally {
        closeQuietly(docOS);
    }

    System.out.printf("bin: %s %d %n", w1, size1);
    System.out.printf("xmi: %s %d %n", w3, size2);
}

From source file:com.jaspersoft.jasperserver.core.util.TolerantObjectWrapper.java

/**
 * tests whether an object is serializable
 *
 * return boolean representing if it is serializable
 *
 * *//*from   w  w  w .j av  a 2s  . c o  m*/
private boolean testIsSerializable(Object obj) throws IOException {

    NullOutputStream nos = new NullOutputStream();
    ObjectOutputStream oos = null;

    try {
        oos = new ObjectOutputStream(nos);
        oos.writeObject(obj);
        oos.close();
        nos.close();

    } catch (Exception err) {
        return false;
    }
    return true;
}

From source file:disko.flow.analyzers.socket.SocketTransmitter.java

public void writeMessage(Message message) {
    log.debug("Establishing connection to " + host + ":" + port);
    while (true) {
        Socket socket = null;//from   w w  w  .  j a  v a 2 s  .  c om
        ObjectOutputStream out = null;
        try {
            socket = new Socket(host, port);
            out = new ObjectOutputStream(socket.getOutputStream());
            out.writeObject(message);
            out.flush();
            log.debug("Sent " + message);
            return;
        } catch (UnknownHostException e) {
            log.error("Error connecting to " + host + ":" + port, e);
        } catch (IOException e) {
            log.error("Error sending to " + host + ":" + port + " message " + message.getData(), e);
        } finally {
            if (out != null)
                try {
                    out.close();
                } catch (Throwable ignored) {
                    log.error("While closing transmitter output.", ignored);
                }
            if (socket != null)
                try {
                    socket.close();
                } catch (Throwable ignored) {
                    log.error("While closing transmitter socket.", ignored);

                }
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ignored) {
            break;
        }
    }
}

From source file:com.textocat.textokit.commons.wfstore.DefaultWordformStore.java

@Override
public void persist(File outFile) throws Exception {
    // serialize store object
    ObjectOutputStream modelOS = null;
    try {/*from  w w  w .  j a  v a  2 s .  c  o m*/
        OutputStream os = new BufferedOutputStream(openOutputStream(outFile));
        modelOS = new ObjectOutputStream(os);
        modelOS.writeObject(this);
    } finally {
        IOUtils.closeQuietly(modelOS);
    }
    log.info("Succesfully serialized to {}, size = {} bytes", outFile, outFile.length());
}

From source file:com.bitranger.parknshop.util.ObjUtils.java

public static byte[] toBytes(Object obj) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream objOut = null;
    byte[] plainObj = null;

    try {//from  ww w.java  2 s .co  m
        objOut = new ObjectOutputStream(baos);
        objOut.writeObject(obj);
        plainObj = baos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage() + "\n\tCAUSE: " + e.getCause(), e);
    } finally {
        try {
            baos.close();
            if (objOut != null) {
                objOut.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage() + "\n\tCAUSE: " + e.getCause(), e);
        }
    }
    return plainObj;
}

From source file:cpd3314.buildit12.CPD3314BuildIt12.java

/**
 * Build a sample method that saves a handful of car instances as Serialized
 * objects, and as JSON objects./*ww w. j ava  2s  .c o m*/
 */
public static void doBuildIt2Output() {
    Car[] cars = { new Car("Honda", "Civic", 2001), new Car("Buick", "LeSabre", 2003),
            new Car("Toyota", "Camry", 2005) };

    // Saves as Serialized Objects
    try {
        FileOutputStream objFile = new FileOutputStream("cars.obj");
        ObjectOutputStream objStream = new ObjectOutputStream(objFile);

        for (Car car : cars) {
            objStream.writeObject(car);
        }

        objStream.close();
        objFile.close();
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Saves as JSONObject
    try {
        PrintWriter jsonStream = new PrintWriter("cars.json");

        JSONArray arr = new JSONArray();
        for (Car car : cars) {
            arr.add(car.toJSON());
        }

        JSONObject json = new JSONObject();
        json.put("cars", arr);

        jsonStream.println(json.toJSONString());

        jsonStream.close();
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }
}