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:PersisTest.java

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == clearBtn) {
        // Repaint with an empty display list.
        displayList = new ArrayList();
        repaint();/*  w w  w .j  a  v a2s  .  c om*/
    } else if (e.getSource() == saveBtn) {
        // Write display list array list to an object output stream.
        try {
            FileOutputStream fos = new FileOutputStream(pathname);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(displayList);
            oos.flush();
            oos.close();
            fos.close();
        } catch (IOException ex) {
            System.err.println("Trouble writing display list array list");
        }
    } else if (e.getSource() == restoreBtn) {
        // Read a new display list array list from an object input stream.
        try {
            FileInputStream fis = new FileInputStream(pathname);
            ObjectInputStream ois = new ObjectInputStream(fis);
            displayList = (ArrayList) (ois.readObject());
            ois.close();
            fis.close();
            repaint();
        } catch (ClassNotFoundException ex) {
            System.err.println("Trouble reading display list array list");
        } catch (IOException ex) {
            System.err.println("Trouble reading display list array list");
        }
    } else if (e.getSource() == quitBtn) {
        System.exit(0);
    }
}

From source file:de.tud.cs.se.flashcards.persistence.Store.java

public static void saveSeries(FlashcardSeries series, File file) throws IOException {

    ObjectOutputStream oout = null;
    try {/*  w  ww  .  ja va2  s .co  m*/
        oout = new ObjectOutputStream(new FileOutputStream(file));
        oout.writeInt(series.getSize());
        for (int i = series.getSize() - 1; i >= 0; i -= 1) {
            oout.writeObject(series.getElementAt(i));
        }
    } finally {
        if (oout != null)
            oout.close();
    }

}

From source file:MSUmpire.SpectrumParser.DIA_Setting.java

public void WriteDIASettingSerialization(String mzXMLFileName) {
    try {//from w w  w .  j a v  a  2s .  com
        Logger.getRootLogger().info("Writing DIA setting to file:" + FilenameUtils.getFullPath(mzXMLFileName)
                + FilenameUtils.getBaseName(mzXMLFileName) + "_diasetting.ser...");
        FileOutputStream fout = new FileOutputStream(FilenameUtils.getFullPath(mzXMLFileName)
                + FilenameUtils.getBaseName(mzXMLFileName) + "_diasetting.ser", false);
        ObjectOutputStream oos = new ObjectOutputStream(fout);
        oos.writeObject(this);
        oos.close();
        fout.close();
    } catch (Exception ex) {
        Logger.getRootLogger().error(ExceptionUtils.getStackTrace(ex));
    }
}

From source file:com.cyberway.issue.crawler.datamodel.CrawlURITest.java

/**
 * Test serialization/deserialization works.
 * /*w  ww .ja  v  a  2s  .  co  m*/
 * @throws IOException
 * @throws ClassNotFoundException
 */
final public void testSerialization() throws IOException, ClassNotFoundException {
    File serialize = new File(getTmpDir(), this.getClass().getName() + ".serialize");
    try {
        FileOutputStream fos = new FileOutputStream(serialize);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(this.seed);
        oos.reset();
        oos.writeObject(this.seed);
        oos.reset();
        oos.writeObject(this.seed);
        oos.close();
        // Read in the object.
        FileInputStream fis = new FileInputStream(serialize);
        ObjectInputStream ois = new ObjectInputStream(fis);
        CrawlURI deserializedCuri = (CrawlURI) ois.readObject();
        deserializedCuri = (CrawlURI) ois.readObject();
        deserializedCuri = (CrawlURI) ois.readObject();
        assertTrue("Deserialized not equal to original",
                this.seed.toString().equals(deserializedCuri.toString()));
        String host = this.seed.getUURI().getHost();
        assertTrue("Deserialized host not null", host != null && host.length() >= 0);
    } finally {
        serialize.delete();
    }
}

From source file:com.feedzai.fos.impl.weka.utils.Cloner.java

/**
 * Creates a clonner from the given object.
 *
 * @param serializable the object to clone
 * @throws IOException when there were problems serializing the object
 *//* w w  w  . j  av a2 s  .  com*/
public Cloner(T serializable) throws IOException {
    checkNotNull(serializable, "The serialized object cannot be null");

    ObjectOutputStream oos = null;
    ByteArrayOutputStream baos = null;

    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(serializable);
        oos.flush();

        this.serializedObject = baos.toByteArray();
    } finally {
        IOUtils.closeQuietly(baos);
        IOUtils.closeQuietly(oos);
    }
}

From source file:com.ecyrd.jspwiki.util.Serializer.java

/**
 * Serializes a Map and formats it into a Base64-encoded String. For ease of serialization, the Map contents
 * are first copied into a HashMap, then serialized into a byte array that is encoded as a Base64 String.
 * @param map the Map to serialize/*from  www .jav a2 s  .c  om*/
 * @return a String representing the serialized form of the Map
 * @throws IOException If serialization cannot be done
 */
public static String serializeToBase64(Map<String, Serializable> map) throws IOException {
    // Load the Map contents into a defensive HashMap
    HashMap<String, Serializable> serialMap = new HashMap<String, Serializable>();
    serialMap.putAll(map);

    // Serialize the Map to an output stream
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bytesOut);
    out.writeObject(serialMap);
    out.close();

    // Transform to Base64-encoded String
    byte[] result = Base64.encodeBase64(bytesOut.toByteArray());
    return new String(result);
}

From source file:com.fuzhepan.arpc.client.ProxyHandler.java

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    log.debug("invoke was called!");

    if (method.getName().equals("toString")) {
        return "toString method was called";
    }//from   w w w.j  a v a2  s  .  c  o  m

    RpcContext rpcContext = new RpcContext(interfaceName, method.getName(), method.getParameterTypes(), args);

    //get service info and load balance
    List<HostPortPair> serviceList = RpcConfig.getServiceList(serviceName);
    if (serviceList == null || serviceList.size() == 0)
        throw new ClassNotFoundException("not find service : " + serviceName);
    int index = requestCount.get() % serviceList.size();
    if (requestCount.get() > 100)
        requestCount.set(0);
    else
        requestCount.getAndIncrement();
    HostPortPair hostPort = serviceList.get(index);

    Socket socket = new Socket(hostPort.host, hostPort.port);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
    objectOutputStream.writeObject(rpcContext);
    objectOutputStream.flush();

    ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
    Object response = objectInputStream.readObject();

    objectInputStream.close();
    objectOutputStream.close();
    socket.close();

    Class methodReturnType = method.getReturnType();
    return methodReturnType.cast(response);
}

From source file:cascading.util.Util.java

public static String serializeBase64(Object object, boolean compress) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

    ObjectOutputStream out = new ObjectOutputStream(compress ? new GZIPOutputStream(bytes) : bytes);

    try {//from ww w.  j a  v a 2  s  .  c o m
        out.writeObject(object);
    } finally {
        out.close();
    }

    return new String(Base64.encodeBase64(bytes.toByteArray()));
}

From source file:core.service.bus.socket.SocketWrapper.java

public ObjectOutputStream getObjectOutputStream() throws IOException {
    synchronized (this) {
        if (output == null) {
            output = new ObjectOutputStream(socket.getOutputStream());
        }/*from www  .j  a  v  a  2 s  .c o  m*/
    }
    return output;
}

From source file:com.px100systems.data.utility.BackupFile.java

public void write(RawRecord record) throws IOException {
    if (os == null)
        os = new ObjectOutputStream(new FileOutputStream(file));
    os.writeObject(record);/*from w ww.  ja v  a 2  s  .c om*/
}