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:com.centurylink.mdw.common.translator.impl.JavaObjectTranslator.java

public String realToString(Object object) throws TranslationException {
    if (!(object instanceof Serializable))
        throw new TranslationException("Object must implement java.io.Serializable: " + object.getClass());

    ObjectOutputStream oos = null;
    try {/*from w  w  w  . j  av  a  2 s  .com*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        return encodeBase64(baos.toByteArray());
    } catch (IOException ex) {
        throw new TranslationException(ex.getMessage(), ex);
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException ex) {
            }
        }
    }
}

From source file:org.jfree.chart.demo.SerializationTest1.java

/**
 * Constructs a new demonstration application.
 *
 * @param title  the frame title.//from   w  w w  . j  a  v a2 s  .  co m
 */
public SerializationTest1(final String title) {

    super(title);
    this.series = new TimeSeries("Random Data", Millisecond.class);
    TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
    JFreeChart chart = createChart(dataset);

    // SERIALIZE - DESERIALIZE for testing purposes
    JFreeChart deserializedChart = null;

    try {
        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        final ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(chart);
        out.close();
        chart = null;
        dataset = null;
        this.series = null;
        System.gc();

        final ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        deserializedChart = (JFreeChart) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    final TimeSeriesCollection c = (TimeSeriesCollection) deserializedChart.getXYPlot().getDataset();
    this.series = c.getSeries(0);
    // FINISHED TEST

    final ChartPanel chartPanel = new ChartPanel(deserializedChart);
    final JButton button = new JButton("Add New Data Item");
    button.setActionCommand("ADD_DATA");
    button.addActionListener(this);

    final JPanel content = new JPanel(new BorderLayout());
    content.add(chartPanel);
    content.add(button, BorderLayout.SOUTH);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(content);

}

From source file:com.antbrains.crf.hadoop.InstanceGenerator.java

public static String object2String(Object o) throws IOException {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream so = new ObjectOutputStream(bo);
    so.writeObject(o);//from w  w  w .  j a  v a  2s . c  o m
    so.flush();
    byte[] arr = Base64.encodeBase64(bo.toByteArray());
    return new String(arr, "UTF8");
}

From source file:ddf.catalog.cache.impl.FileSystemPersistenceProvider.java

@Override
public void store(String key, Object value) {
    OutputStream file = null;/* www. ja v  a  2s.co m*/
    ObjectOutput output = null;

    LOGGER.trace("Entering: store - key: {}", key);
    try {
        File dir = new File(getMapStorePath());
        if (!dir.exists()) {
            boolean success = dir.mkdir();
            if (!success) {
                LOGGER.error("Could not make directory: {}", dir.getAbsolutePath());
            }
        }
        LOGGER.debug("file name: {}{}{}", getMapStorePath(), key, SER);
        file = new FileOutputStream(getMapStoreFile(key));
        OutputStream buffer = new BufferedOutputStream(file);
        output = new ObjectOutputStream(buffer);
        output.writeObject(value);
    } catch (IOException e) {
        LOGGER.info("IOException storing value in cache with key = {}", key, e);
    } finally {
        try {
            if (output != null) {
                output.close();
            }
        } catch (IOException e) {
            // Intentionally ignored
        }
        IOUtils.closeQuietly(file);
    }
    LOGGER.trace("Exiting: store");
}

From source file:com.l2jfree.gameserver.instancemanager.GameTimeManager.java

private void saveData() {
    if (!Config.DATETIME_SAVECAL)
        return;//from www.  j a va  2 s.c  om

    ObjectOutputStream os = null;
    try {
        os = new ObjectOutputStream(new FileOutputStream("data/serial/clock.dat"));

        os.writeObject(_calendar);
    } catch (IOException e) {
        _log.warn("", e);
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:com.conwet.silbops.model.AdvertiseTest.java

@Test
public void shouldExternalize() throws Exception {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutput output = new ObjectOutputStream(baos);

    output.writeObject(advertise);//w ww. ja  va2  s  . c o m
    output.close();

    ObjectInput input = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));

    assertThat((Advertise) input.readObject()).isEqualTo(advertise);
}

From source file:com.joliciel.talismane.machineLearning.perceptron.PerceptronClassificationModel.java

@Override
public void writeModelToStream(OutputStream outputStream) {
    try {/*from w  w w.  j av a2s.  c  o m*/
        ObjectOutputStream out = new ObjectOutputStream(outputStream);

        out.writeObject(params);
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:hudson.cli.Connection.java

/**
 * Sends a serializable object.//from w  w w . j a  v  a  2  s. c o  m
 */
public void writeObject(Object o) throws IOException {
    ObjectOutputStream oos = new ObjectOutputStream(out);
    oos.writeObject(o);
    // don't close oss, which will close the underlying stream
    // no need to flush either, given the way oos is implemented
}

From source file:TcpClient.java

/** Default constructor. */
public TcpServer()
{
    this.payload = new TcpPayload();
    initServerSocket();/*from  ww w.ja va2  s. c o m*/
    try
    {
        while (true)
        {
            // listen for and accept a client connection to serverSocket
            Socket sock = this.serverSocket.accept();
            OutputStream oStream = sock.getOutputStream();
            ObjectOutputStream ooStream = new ObjectOutputStream(oStream);
            ooStream.writeObject(this.payload);  // send serilized payload
            ooStream.close();
            Thread.sleep(1000);
        }
    }
    catch (SecurityException se)
    {
        System.err.println("Unable to get host address due to security.");
        System.err.println(se.toString());
        System.exit(1);
    }
    catch (IOException ioe)
    {
        System.err.println("Unable to read data from an open socket.");
        System.err.println(ioe.toString());
        System.exit(1);
    }
    catch (InterruptedException ie) { }  // Thread sleep interrupted
    finally
    {
        try
        {
            this.serverSocket.close();
        }
        catch (IOException ioe)
        {
            System.err.println("Unable to close an open socket.");
            System.err.println(ioe.toString());
            System.exit(1);
        }
    }
}

From source file:org.jfree.chart.demo.ChartPanelSerializationTest.java

/**
 * A demonstration application showing how to create a simple time series chart.  This
 * example uses monthly data./*from  w w w  .j  a v a 2  s .  c om*/
 *
 * @param title  the frame title.
 */
public ChartPanelSerializationTest(final String title) {

    super(title);
    final XYDataset dataset = createDataset();
    final JFreeChart chart = createChart(dataset);
    final ChartPanel chartPanel1 = new ChartPanel(chart);
    chartPanel1.setPreferredSize(new java.awt.Dimension(500, 270));
    chartPanel1.setMouseZoomable(true, false);

    ChartPanel chartPanel2 = null;
    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(chartPanel1);
        out.close();

        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
        chartPanel2 = (ChartPanel) in.readObject();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    setContentPane(chartPanel2);

}