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:net.sourceforge.jaulp.io.SerializedObjectUtils.java

/**
 * Writes the given object to the given File.
 *
 * @param obj//from  w  ww  .  j a  v  a 2s .c o m
 *            The object to write to the File.
 * @param file
 *            In this file will be the object saved.
 * @return true if the object was written to the file otherwise false.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static boolean writeSerializedObjectToFile(final Object obj, final File file) throws IOException {
    boolean written = true;
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();
    } finally {
        StreamUtils.closeOutputStream(oos);
        StreamUtils.closeOutputStream(fos);
    }
    return written;
}

From source file:com.discovery.darchrow.io.SerializableUtil.java

/**
 * To byte array output stream./*  www .  j  a v a2s .  co  m*/
 *
 * @param serializable
 *            the serializable
 * @return the byte array output stream
 * @see java.io.ObjectOutputStream#ObjectOutputStream(OutputStream)
 * @see java.io.ObjectOutputStream#writeObject(Object)
 * @see org.apache.commons.lang3.SerializationUtils#serialize(Serializable, OutputStream)
 */
private static ByteArrayOutputStream toByteArrayOutputStream(Serializable serializable) {
    ObjectOutputStream objectOutputStream = null;
    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(serializable);

        return byteArrayOutputStream;
    } catch (IOException e) {
        LOGGER.error("", e);
        throw new SerializationException(e);
    } finally {
        IOUtils.closeQuietly(objectOutputStream);
    }
}

From source file:misc.TestUtils.java

@SuppressWarnings("unchecked")
public static <T extends Serializable> T cloneSerializable(T obj) {
    ObjectOutputStream out = null;
    ObjectInputStream in = null;/*from   w w w  . ja v  a 2  s  .c  om*/

    try {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        out = new ObjectOutputStream(bout);

        out.writeObject(obj);
        out.close();

        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        in = new ObjectInputStream(bin);
        Object copy = in.readObject();

        in.close();

        return (T) copy;
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }

            if (in != null) {
                in.close();
            }
        } catch (IOException ignore) {
        }
    }

    return null;
}

From source file:com.ligadata.EncryptUtils.EncryptionUtil.java

/**
 * Generate key which contains a pair of private and public key using 1024
 * bytes. Store the set of keys in given files publicKeyFile,privateKeyFile
 * @param algorithm/*from  w  w  w. j a va 2 s .c  o  m*/
 *          : algorithm used
 * @param publicKeyFile
 *          :The file containing public key
 * @param privateKeyFile
 *          :The file containing private key
 */
public static void generateSampleKeys(String algorithm, String publicKeyFile, String privateKeyFile) {
    try {
        if (areKeysPresent(publicKeyFile, privateKeyFile)) {
            return;
        }
        final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
        keyGen.initialize(1024);
        final KeyPair key = keyGen.generateKeyPair();

        File privateKeyFD = new File(privateKeyFile);
        File publicKeyFD = new File(publicKeyFile);

        // Create files to store public and private key
        if (privateKeyFD.getParentFile() != null) {
            privateKeyFD.getParentFile().mkdirs();
        }
        privateKeyFD.createNewFile();

        if (publicKeyFD.getParentFile() != null) {
            publicKeyFD.getParentFile().mkdirs();
        }
        publicKeyFD.createNewFile();

        // Saving the Public key in a file
        ObjectOutputStream publicKeyOS = new ObjectOutputStream(new FileOutputStream(publicKeyFD));
        publicKeyOS.writeObject(key.getPublic());
        publicKeyOS.close();

        // Saving the Private key in a file
        ObjectOutputStream privateKeyOS = new ObjectOutputStream(new FileOutputStream(privateKeyFD));
        privateKeyOS.writeObject(key.getPrivate());
        privateKeyOS.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:de.alpharogroup.io.SerializedObjectExtensions.java

/**
 * Writes the given object to the given File.
 *
 * @param obj// ww  w .j  a  va2  s .  c  o m
 *            The object to write to the File.
 * @param file
 *            In this file will be the object saved.
 * @return true if the object was written to the file otherwise false.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static boolean writeSerializedObjectToFile(final Object obj, final File file) throws IOException {
    final boolean written = true;
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();
    } finally {
        StreamExtensions.closeOutputStream(oos);
        StreamExtensions.closeOutputStream(fos);
    }
    return written;
}

From source file:de.alpharogroup.io.SerializedObjectUtils.java

/**
 * Writes the given object to the given File.
 *
 * @param obj//from w ww  .  ja  v a 2s.  c o  m
 *            The object to write to the File.
 * @param file
 *            In this file will be the object saved.
 * @return true if the object was written to the file otherwise false.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static boolean writeSerializedObjectToFile(final Object obj, final File file) throws IOException {
    final boolean written = true;
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();
    } finally {
        StreamUtils.closeOutputStream(oos);
        StreamUtils.closeOutputStream(fos);
    }
    return written;
}

From source file:com.dev.pygmy.util.Utils.java

/**
 * Saves the current game by serializing it and storing in locally
 * on the device/*from   ww  w .  j  a  v  a2  s  .co  m*/
 */
public static void saveGame(PygmyGame game, String path) {
    ObjectOutputStream oos = null;
    try {
        File history = new File(path);
        history.getParentFile().createNewFile();
        FileOutputStream fout = new FileOutputStream(history);
        oos = new ObjectOutputStream(fout);
        oos.writeObject(game);
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (oos != null) {
                oos.flush();
                oos.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:za.co.neilson.alarm.database.Database.java

public static int update(Alarm alarm) {
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_ALARM_ACTIVE, alarm.getAlarmActive());
    cv.put(COLUMN_ALARM_TIME, alarm.getAlarmTimeString());

    try {//from w w  w. ja  va 2 s . c  o  m
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        oos = new ObjectOutputStream(bos);
        oos.writeObject(alarm.getDays());
        byte[] buff = bos.toByteArray();

        cv.put(COLUMN_ALARM_DAYS, buff);

    } catch (Exception e) {
    }

    cv.put(COLUMN_ALARM_DIFFICULTY, alarm.getDifficulty().ordinal());
    cv.put(COLUMN_ALARM_TONE, alarm.getAlarmTonePath());
    cv.put(COLUMN_ALARM_VIBRATE, alarm.getVibrate());
    cv.put(COLUMN_ALARM_NAME, alarm.getAlarmName());

    return getDatabase().update(ALARM_TABLE, cv, "_id=" + alarm.getId(), null);
}

From source file:za.co.neilson.alarm.database.Database.java

public static long create(Alarm alarm) {
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_ALARM_ACTIVE, alarm.getAlarmActive());
    cv.put(COLUMN_ALARM_TIME, alarm.getAlarmTimeString());

    try {/*  w  ww. j a v a 2 s  .  co m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        oos = new ObjectOutputStream(bos);
        oos.writeObject(alarm.getDays());
        byte[] buff = bos.toByteArray();
        cv.put(COLUMN_ALARM_DAYS, buff);

    } catch (Exception e) {
    }
    Log.v("Alarm Active : ", "" + alarm.getAlarmActive());
    Log.v("Alarm Name : ", "" + alarm.getAlarmName());

    cv.put(COLUMN_ALARM_DIFFICULTY, alarm.getDifficulty().ordinal());
    cv.put(COLUMN_ALARM_TONE, alarm.getAlarmTonePath());
    cv.put(COLUMN_ALARM_VIBRATE, alarm.getVibrate());
    cv.put(COLUMN_ALARM_NAME, alarm.getAlarmName());

    return getDatabase().insert(ALARM_TABLE, null, cv);
}

From source file:za.co.neilson.alarm.database.Database.java

public static long create(String name, String time, String tone, int difficulty, boolean vibrate,
        String[] days) {/* w w  w.ja  v a  2s .c  o  m*/
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_ALARM_ACTIVE, true);
    cv.put(COLUMN_ALARM_TIME, time);
    Alarm.Day[] enumDays = new Alarm.Day[days.length];

    for (int i = 0; i < days.length; i++) {
        enumDays[i] = Alarm.Day.valueOf(days[i]);
    }

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        oos = new ObjectOutputStream(bos);
        oos.writeObject(enumDays);
        byte[] buff = bos.toByteArray();

        cv.put(COLUMN_ALARM_DAYS, buff);

    } catch (Exception e) {
    }

    cv.put(COLUMN_ALARM_DIFFICULTY, difficulty);
    cv.put(COLUMN_ALARM_TONE, tone);
    cv.put(COLUMN_ALARM_VIBRATE, vibrate);
    cv.put(COLUMN_ALARM_NAME, name);

    return getDatabase().insert(ALARM_TABLE, null, cv);
}