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

public static void writeSObject(Context context, Object content, String path) {

    try {/* w  ww  . j a  v  a  2 s.c o  m*/
        ByteArrayOutputStream e = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(e);
        oos.writeObject(content);
        String stringBase64 = new String(Base64.encode(e.toByteArray(), 0));
        writeString(context, stringBase64, path);
    } catch (IOException var6) {
        var6.printStackTrace();
    }

}

From source file:Main.java

public static boolean saveBeanToFile(String filePath, Object bean) {
    File file = new File(filePath);
    ObjectOutputStream outputStream = null;
    if (file.exists()) {
        file.delete();/*from  w w w .ja va 2s  . c om*/
    }

    try {
        outputStream = new ObjectOutputStream(new FileOutputStream(file));
        outputStream.writeObject(bean);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeQuiltly(outputStream);
    }

    return true;
}

From source file:Util.java

public static byte[] compress(Object data) {
    if (data == null) {
        return null;
    }/*ww  w .j av  a  2  s.c o m*/
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gout = new GZIPOutputStream(baos);
        ObjectOutputStream oos = new ObjectOutputStream(gout);
        oos.writeObject(data);
        oos.flush();
        gout.finish();
        return baos.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void put_(String key, Serializable value) {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;
    try {/*from  w w w . j  av a  2 s  .c om*/
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(value);
        byte[] data = baos.toByteArray();
        put(key, data);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            oos.close();
        } catch (IOException e) {
        }
    }
}

From source file:Main.java

/**
 * Convert serializable object to bytes.
 *
 * @param object object//from   w w w .j  a  v  a  2  s.  c  o  m
 * @return bytes array
 */
public static byte[] toBytes(Object object) {
    byte[] result = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(object);
        result = bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
            }
        }
        if (bos != null) {
            try {
                bos.close();
            } catch (Exception e) {
            }
        }
    }
    return result;
}

From source file:Main.java

public static boolean saveObject(@NonNull Context context, Object obj, String fileName) {
    if (obj == null || TextUtils.isEmpty(fileName)) {
        return false;
    }/* w  w w. j  a va2  s .  c  om*/

    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (fos != null)
                fos.close();
            if (oos != null)
                oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:BytesUtil.java

public static byte[] toByteArray(Object obj) throws IOException {
    byte[] bytes = null;
    ByteArrayOutputStream bos = null;
    ObjectOutputStream oos = null;
    try {//  www . ja v  a  2s.  c  o m
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        bytes = bos.toByteArray();
    } finally {
        if (oos != null) {
            oos.close();
        }
        if (bos != null) {
            bos.close();
        }
    }
    return bytes;
}

From source file:Main.java

public static void saveSeriObj(Context context, String fileName, Object o) throws Exception {

    String path = context.getFilesDir() + "/";

    File dir = new File(path);
    dir.mkdirs();/*from  ww w .  j  a  v  a  2 s.co  m*/

    File f = new File(dir, fileName);

    if (f.exists()) {
        f.delete();
    }
    FileOutputStream os = new FileOutputStream(f);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
    objectOutputStream.writeObject(o);
    objectOutputStream.close();
    os.close();
}

From source file:Main.java

public static final void SaveObject(final String path, final Object saveObject) {
    FileOutputStream fileOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    File file = new File(path);
    try {//from w  ww  .j a v  a 2s  .co m
        if (!file.exists()) {
            file.createNewFile();
        }
        fileOutputStream = new FileOutputStream(file);
        objectOutputStream = new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(saveObject);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (objectOutputStream != null) {
                objectOutputStream.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.quangphuong.crawler.util.Agent.java

public static long sizeOf(Serializable object) {
    if (object == null) {
        return 0;
    }//from   ww  w.  j  a v  a2  s  . c om

    try {
        final ByteCountingOutputStream out = new ByteCountingOutputStream();
        new ObjectOutputStream(out).writeObject(object);
        out.close();
        return out.size();
    } catch (IOException e) {
        return -1;
    }
}