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.ebay.erl.mobius.util.SerializableUtil.java

public final static String serializeToBase64(Serializable obj) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {//from  w w  w.  j av  a2 s .  c om
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();

        String result = new String(Base64.encodeBase64(bos.toByteArray()), "UTF-8");
        return result;
    } catch (NotSerializableException e) {
        throw new IllegalArgumentException("Cannot serialize " + obj.getClass().getCanonicalName(), e);
    } finally {
        try {
            bos.close();
        } catch (Throwable e) {
            e = null;
        }

        try {
            if (oos != null)
                oos.close();
        } catch (Throwable e) {
            e = null;
        }
    }
}

From source file:homemade.apps.framework.homerlibs.utils.ObjectSerializer.java

public static String serialize(Serializable obj) {
    if (obj == null)
        return "";
    try {// w  w w  . ja  va  2s  .  c  o  m
        ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
        ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
        objStream.writeObject(obj);
        objStream.close();
        return encodeBytes(serialObj.toByteArray());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mErrorReturn;
}

From source file:Main.java

/**
 * Serialize an object./* ww w  .ja  va2s  .com*/
 * @param obj a serializable object.
 * @return a byte array of the serialized object or null if an error occurs.
 */
public static byte[] serialize(Object obj) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.flush();
        baos.flush();
        return baos.toByteArray();
    } catch (IOException e) {
        return null;
    } finally {
        try {
            if (oos != null)
                oos.close();
        } catch (IOException e) {
        }
    }
}

From source file:edu.umd.umiacs.clip.tools.io.SerializationTools.java

public static void serializeAndOverride(String path, Object object) {
    String tmp = path + "._SAVING";
    new File(tmp).delete();
    new File(tmp).getParentFile().mkdirs();
    try (FileOutputStream os = new FileOutputStream(tmp);
            ObjectOutputStream out = new ObjectOutputStream(
                    path.endsWith(".bz2") ? new BZip2CompressorOutputStream(os)
                            : path.endsWith(".gz") ? new GzipCompressorOutputStream(os) : os)) {
        out.writeObject(object);//w  ww  . ja va  2  s.  c  om
    } catch (IOException e) {
        e.printStackTrace();
    }
    new File(path).delete();
    new File(tmp).renameTo(new File(path));
}

From source file:Main.java

private static byte[] serializable2Bytes(final Serializable serializable) {
    if (serializable == null)
        return null;
    ByteArrayOutputStream baos;//www  .j  a  v  a2 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:edu.virginia.jtd5qe.twitter.ObjectSerializer.java

public static String serialize(Serializable obj) throws IOException {
    if (obj == null)
        return "";
    try {//  w w  w  .  j ava  2s .  com
        ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
        ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
        objStream.writeObject(obj);
        objStream.close();
        return encodeBytes(serialObj.toByteArray());
    } catch (Exception e) {
        throw WrappedIOException.wrap("Serialization error: " + e.getMessage(), e);
    }
}

From source file:com.trin.nilmobile.serializer.ObjectSerializer.java

public static String serialize(Serializable obj) throws IOException {
    if (obj == null)
        return "";
    try {//  w  w w  .  ja  v a  2s.  c o  m
        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:berlin.iconn.persistence.InOutOperations.java

public static void saveSimpleWeights(float[][] weights, Date date, String suffix) throws IOException {
    mkdir(simpleWeightsFolder);/*  w w w. ja v  a 2 s . c om*/
    File file = new File(simpleWeightsFolder + "/" + getFileNameByDate(date, suffix, "dat"));
    ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(file.toPath()));
    oos.writeObject(weights);
    oos.close();
}

From source file:it.restrung.rest.utils.IOUtils.java

/**
 * Serializes ans saves a Serializable object to a file
 *
 * @param object the source object/*from w  w  w  . ja va  2  s. com*/
 * @param file   the target file
 */
static public void saveSerializableObjectToDisk(Object object, File file) {
    try {
        file.delete();
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        GZIPOutputStream gzos = new GZIPOutputStream(fos);
        ObjectOutputStream out = new ObjectOutputStream(gzos);
        out.writeObject(object);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        Log.d(IOUtils.class.getName(), "Error, file not found for save serializable object to disk");
        throw new RuntimeException(e);
    } catch (IOException e) {
        Log.d(IOUtils.class.getName(), "Error on save serializable object");
        throw new RuntimeException(e);
    }
}

From source file:gnomezgrave.gsyncj.auth.ProfileSettings.java

public static void saveSettings(ProfileSettings settings, String fileName)
        throws IOException, ClassNotFoundException {
    ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(fileName));
    oo.writeObject(settings);//from   w  w  w  . j av  a2 s.c o  m
    oo.close();
}