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

public static void SerializeObject(Object object, String path) throws IOException {
    ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(new File(path)));
    oo.writeObject(object);
    oo.close();//  w  ww.  j a v  a 2  s.  c o  m
}

From source file:Main.java

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

    try {//from  w w  w.  ja  v  a 2  s . c om
        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 String objectToString(Object obj) {
    try {/*from   w  w  w.  j av  a2 s  . c o m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(bos);
        os.writeObject(obj);
        String bytesToHexString = bytesToHexString(bos.toByteArray());
        return bytesToHexString;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:MainClass.java

public static void desEncrypt(String f1, String f2) throws Exception {
    SecretKey key = null;/*from w  ww  .ja  va  2  s. c  o m*/
    ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("DESKey.ser"));
    key = (SecretKey) keyFile.readObject();
    keyFile.close();

    KeyGenerator keygen = KeyGenerator.getInstance("DES");
    key = keygen.generateKey();
    ObjectOutputStream keyFileout = new ObjectOutputStream(new FileOutputStream("DESKey.ser"));
    keyFileout.writeObject(key);
    keyFileout.close();
    Cipher cipher = null;
    cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(f1));

    CipherOutputStream out = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(f2)), cipher);
    int i;
    do {
        i = in.read();
        if (i != -1)
            out.write(i);
    } while (i != -1);
    in.close();
    out.close();
}

From source file:Main.java

/**
 * Sends an object through bluetooth./*w  w  w .j a  v a  2 s  . c o m*/
 */
public static boolean bluetoothWriteObject(Object object, BluetoothSocket socket) {
    try {
        ObjectOutputStream oOS = new ObjectOutputStream(socket.getOutputStream());
        oOS.writeObject(object);
        oOS.flush();
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

public static String objectToString(Serializable serializableObject) throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(serializableObject);
    objectOutputStream.close();/*from  ww  w. ja va2  s . c  o m*/
    return new String(Base64.encode(byteArrayOutputStream.toByteArray(), 0));
}

From source file:SerializationUtils.java

public static byte[] serialize(Object obj) {
    try {//  w  ww  . ja  v a 2  s .c  om
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(buffer);
        oos.writeObject(obj);
        oos.close();
        return buffer.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("error writing to byte-array!");
    }
}

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();/*ww  w .  ja  va2  s. c om*/

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

public static byte[] compress(Object data) {
    if (data == null) {
        return null;
    }/*from  w ww .  j a  v a 2 s .  co  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 <T> List<T> deepCopy(List<T> src) {
    try {//from ww  w.  j a v  a2s .  c o  m
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);

        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        @SuppressWarnings("unchecked")
        List<T> dest = (List<T>) in.readObject();
        return dest;
    } catch (Exception e) {
        return null;
    }
}