Example usage for java.io ObjectOutputStream writeObject

List of usage examples for java.io ObjectOutputStream writeObject

Introduction

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

Prototype

public final void writeObject(Object obj) throws IOException 

Source Link

Document

Write the specified object to the ObjectOutputStream.

Usage

From source file:Main.java

private static byte[] serializable2Bytes(final Serializable serializable) {
    if (serializable == null)
        return null;
    ByteArrayOutputStream baos;/*from   w  w  w.  ja  va  2  s.  com*/
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos = new ByteArrayOutputStream());
        oos.writeObject(serializable);
        return baos.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static boolean saveObject(Context context, Object data, String filename) {
    FileOutputStream out;//w ww.ja  v a2  s . c  o  m
    ObjectOutputStream oout;
    try {
        out = context.openFileOutput(filename + ".odb", Context.MODE_PRIVATE);
        oout = new ObjectOutputStream(out);
        oout.writeObject(data);
        oout.flush();
        out.flush();
        oout.close();
        out.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:io.smartspaces.scheduling.quartz.orientdb.internal.util.SerialUtils.java

public static byte[] serialize(Calendar calendar) throws JobPersistenceException {
    // ToDO(keith): Serialize better than Java serialization.
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    try {/*  w w w  .  j  a  va2s  .  c  o  m*/
        ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
        objectStream.writeObject(calendar);
        objectStream.close();
        return byteStream.toByteArray();
    } catch (IOException e) {
        throw new JobPersistenceException("Could not serialize Calendar.", e);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.keyphrases.bookindexing.util.SerializationUtils.java

/**
 * @param object// www .  j av a2s .  c  o  m
 *          object to be serialized
 * @param filePath
 *          the object will be written at this location, directories will be
 *          created according to path
 * @throws Exception exception
 */
public static void serializeToDisk(Serializable object, String filePath) throws Exception {
    File dir = new File(FilenameUtils.getFullPathNoEndSeparator(filePath));
    if (!dir.exists()) {
        FileUtils.forceMkdir(dir);
    } else {
        if (dir.isFile()) {
            throw new IOException("Path to dir is a file!");
        }
    }
    ObjectOutputStream objOut = null;
    objOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filePath)));
    objOut.writeObject(object);
    objOut.flush();
    objOut.close();
}

From source file:com.dragome.callbackevictor.serverside.utils.ReflectionUtils.java

public static Object cast(Object o) throws IOException, ClassNotFoundException {
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(buffer);
    oos.writeObject(o);
    oos.flush();//from   w w w. j a  v  a2  s.c  om
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    return ois.readObject();
}

From source file:Main.java

public static boolean saveObject(Context cxt, Serializable obj, String file) {
    FileOutputStream fos = null;//from w w w  .  ja v  a2 s.c o m
    ObjectOutputStream oos = null;

    try {
        fos = cxt.openFileOutput(file, Context.MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            oos.close();
        } catch (Exception e) {
        }
        try {
            fos.close();
        } catch (Exception e) {
        }
    }
}

From source file:com.cloudera.recordservice.hcatalog.common.HCatRSUtil.java

public static String serialize(Serializable obj) throws IOException {
    if (obj == null) {
        return "";
    }/* w w w  .  j  a v  a2  s .  c o m*/
    try {
        ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
        ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
        objStream.writeObject(obj);
        objStream.close();
        return encodeBytes(serialObj.toByteArray());
    } catch (Exception e) {
        throw new IOException("Serialization error: " + e.getMessage(), e);
    }
}

From source file:com.db4o.sync4o.SyncKey.java

static public String toEncodedString(SyncKey key) throws Exception {

    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(bs);
    os.writeObject(key);
    os.flush();//  w w  w.java2s . c  o m
    os.close();

    return new String(Base64.encodeBase64(bs.toByteArray()));

}

From source file:edu.cmu.cs.lti.ark.util.SerializedObjects.java

public static void writeSerializedObject(Object object, String outFile) {
    ObjectOutputStream output = null;
    try {/*from   w w  w .jav a 2s .c  om*/
        output = getObjectOutputStream(outFile);
        output.writeObject(object);
    } catch (IOException ex) {
        // TODO: NONONONONO! stop swallowing errors!
        ex.printStackTrace();
    } finally {
        closeQuietly(output);
    }
}

From source file:glluch.com.ontotaxoseeker.TestsGen.java

public static String save(Object o, String className) throws FileNotFoundException, IOException {

    if (StringUtils.isEmpty(className)) {
        Out.p("TestsGen.save have received an anonomous object");
        className = "anonimousClass";
    }//from www . j a v  a 2 s  .  co m

    FileOutputStream fileOut = new FileOutputStream("resources/test/" + className + ".ser");
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(o);
    out.close();
    fileOut.close();
    System.out.printf(
            "Serialized data is saved in " + "resources/test/" + className + ".ser" + System.lineSeparator());

    return "resources/test/" + className + ".ser";
}