Example usage for java.io ObjectOutputStream close

List of usage examples for java.io ObjectOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the stream.

Usage

From source file:Main.java

public static void writeDataFile(String filename, Context context, Object object) {
    File file = new File(context.getDir("data", 0), filename);
    try {//from   w w w .j ava2s .  co  m
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file, false));
        outputStream.writeObject(object);
        outputStream.flush();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * serialize to file/* w  w w .  j  av  a2 s . c  om*/
 * 
 * @param filePath
 * @param obj
 * @return
 * @throws RuntimeException if an error occurs
 */
public static void serialization(String filePath, Object obj) {
    ObjectOutputStream out = null;
    try {
        out = new ObjectOutputStream(new FileOutputStream(filePath));
        out.writeObject(obj);
        out.close();
    } catch (FileNotFoundException e) {
        throw new RuntimeException("FileNotFoundException occurred. ", e);
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}

From source file:net.dontdrinkandroot.utils.lang.SerializationUtils.java

@SuppressWarnings("unchecked")
public static <T extends Serializable> T fastClone(T object) {

    Object obj = null;//w  w w.java  2 s.  c o m
    try {

        /* Write the object out to a byte array */
        FastByteArrayOutputStream fbos = new FastByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(fbos);
        out.writeObject(object);
        out.flush();
        out.close();

        /* Retrieve an input stream from the byte array and read a copy of the object back in. */
        ObjectInputStream in = new ObjectInputStream(fbos.getInputStream());
        obj = in.readObject();

    } catch (IOException e) {
        throw new SerializationException(e);
    } catch (ClassNotFoundException cnfe) {
        throw new SerializationException(cnfe);
    }

    return (T) obj;
}

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

public static void saveSettings(Profile profile, String fileName) throws IOException, ClassNotFoundException {
    ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(fileName));
    oo.writeObject(profile);// w w  w .j ava 2 s.c  om
    oo.close();
}

From source file:Util.java

/**
 * Writes a serialized version of obj to a given file, compressing it using gzip.
 * @param f File to write to/*from   ww w  . j a v a  2s . com*/
 * @param obj Object to serialize
 */
public static void writeGzippedObject(File f, Serializable obj) {
    try {
        ObjectOutputStream oos = new ObjectOutputStream(
                new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(f))));
        oos.writeObject(obj);
        oos.close();
    } catch (IOException e) {
        System.err.println("Exception writing file " + f + ": " + e);
    }
}

From source file:Main.java

public static boolean saveObjectToFile(String filePath, Object object) {
    if (!TextUtils.isEmpty(filePath)) {
        File cacheFile = null;//from   w  w  w.  j a va 2  s  .co  m

        try {
            cacheFile = new File(filePath);
            if (cacheFile.exists()) {
                cacheFile.delete();
            }

            if (!cacheFile.getParentFile().exists()) {
                cacheFile.getParentFile().mkdirs();
            }

            cacheFile.createNewFile();
        } catch (Throwable var6) {
            var6.printStackTrace();
            cacheFile = null;
        }

        if (cacheFile != null) {
            try {
                FileOutputStream t = new FileOutputStream(cacheFile);
                GZIPOutputStream gzos = new GZIPOutputStream(t);
                ObjectOutputStream oos = new ObjectOutputStream(gzos);
                oos.writeObject(object);
                oos.flush();
                oos.close();
                return true;
            } catch (Throwable var7) {
                var7.printStackTrace();
            }
        }
    }

    return false;
}

From source file:com.bworld.manager.ObjectSerializer.java

/**
* Serialize.//  w w  w .  java2s.co m
*
* @param obj the obj
* @return the string
*/
public static String serialize(Serializable obj) {
    if (obj == null)
        return "";
    try {
        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);
    }
    return null;
}

From source file:conceptnet.ConceptBuilder.java

public static void saveConcept(Concept c) {
    String path = conceptPath + c.name;
    File f = new File(path);
    if (f.exists() || f.isDirectory()) {
        System.out.println("Try to save a concept that is already saved: " + c.name);
        return;//from   w ww  . j  av  a2s .  c o  m
    }
    try {
        ObjectOutputStream ois = new ObjectOutputStream(new FileOutputStream(path));
        ois.writeObject(c);
        ois.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.mindengine.oculus.frontend.web.Auth.java

public static String encodeUser(User user) throws Exception {
    if (user == null) {
        throw new IllegalArgumentException("User should not be null");
    }//w w w  .  j  av  a  2s . c  om
    if (secrectAuthKey == null) {
        throw new IllegalArgumentException("Couldn't generate secret key");
    }

    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.ENCRYPT_MODE, secrectAuthKey);

    SealedObject sealedUser = new SealedObject(user, cipher);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(sealedUser);
    oos.close();
    return new String(Base64.encodeBase64(baos.toByteArray()));
}

From source file:jp.queuelinker.system.util.SerializeUtil.java

/**
 * Serializes an object to a byte array.
 * @param obj// w  ww . jav  a  2  s  . c o m
 * @return
 */
public static byte[] serializeToBytes(final Object obj) {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    try {
        ObjectOutputStream objStream = new ObjectOutputStream(byteStream);
        objStream.writeObject(obj);
        objStream.close();
    } catch (IOException e) {
        logger.error("Serialization Error. The class " + obj.getClass().getName()
                + " may not implement Serializable interface");
    }
    return byteStream.toByteArray();
}