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 boolean saveObject(Context context, Serializable ser, String file) {
    FileOutputStream fos = null;//from   ww  w  . j av  a2 s.c  om
    ObjectOutputStream oos = null;
    try {
        fos = context.openFileOutput(file, Context.MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(ser);
        oos.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            oos.close();
        } catch (Exception e) {
        }
        try {
            fos.close();
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

/**
 * Returns a byte array from the given object.
 * /*  w w  w  .  jav a2 s . com*/
 * @param object
 *            to convert
 * @return byte array from the object
        
 */
public static byte[] objectToBytes(Object object) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(baos);
    os.writeObject(object);
    return baos.toByteArray();
}

From source file:bencoding.securely.Converters.java

public static String serializeObjectToString(Object object) throws Exception {

    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(arrayOutputStream);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOutputStream);

    objectOutputStream.writeObject(object);

    objectOutputStream.flush();/*w w  w  .  ja v a  2 s .  c  o  m*/
    objectOutputStream.close();
    gzipOutputStream.close();
    arrayOutputStream.close();

    String objectString = new String(Base64.encodeToString(arrayOutputStream.toByteArray(), Base64.DEFAULT));

    return objectString;
}

From source file:Main.java

public static byte[] getBytes(Serializable obj) {
    byte[] bytes = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {//  w ww .  j  av a 2s. c  o  m
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        bytes = bos.toByteArray();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (oos != null) {
            try {
                oos.close();
                bos.close();
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    return bytes;
}

From source file:com.eviware.loadui.util.serialization.SerializationUtils.java

public static byte[] serialize(Object object) throws IOException {
    if (object == null)
        return null;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(object);/* www.ja  v a  2 s  . c  o  m*/
    oos.flush();

    return baos.toByteArray();
}

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  .j  a v a2s  .  co 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:Main.java

/**
 * This method serializes an object to an output stream.
 *
 * @param file The output file/*  w ww .ja v a2  s  .co  m*/
 * @param object The object to be serialized
 * @exception IOException IOError
        
 */
public static void serializeObject(File file, Object object) throws IOException {
    FileOutputStream fos = new FileOutputStream(file);
    try {
        ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos));
        oos.writeObject(object);
        oos.flush();
    } finally {
        fos.close();
    }
}

From source file:llc.rockford.webcast.StringUtils.java

public static String encodeBase64BinaryFile(InputStream is) {
    String base64EncodedFile = "";
    try {//w ww .  j a  va2 s  .co m
        byte[] bytesFromFile = IOUtils.toByteArray(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream stream = new ObjectOutputStream(baos);
        stream.write(bytesFromFile);
        stream.flush();
        stream.close();
        base64EncodedFile = new String(Base64.encodeBase64(baos.toByteArray()));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return base64EncodedFile;
}

From source file:com.test.todolist.ObjectSerializer.java

public static String serialize(Serializable obj) throws IOException {
    if (obj == null)
        return "";
    try {/*w w w.  j a  va2s . co m*/
        ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
        ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
        objStream.writeObject(obj);
        objStream.close();
        return encodeBytes(serialObj.toByteArray());
    } catch (Exception e) {
        return "exception";
    }
}

From source file:Util.java

/**
  * Serializes an object to a file, masking out annoying exceptions.
  *  Any IO exceptions are caught, and printed to standard error.
  *  Consider using {@link #writeGzippedObject(java.io.File, java.io.Serializable)}
  *  instead, for that method will compress the serialized file, and it'll still
  * be reaoable by {@link #readObject}./*from w  w  w  .  j a v a  2 s .  c  om*/
  * @param f File to write to
  * @param obj Object to serialize
  * @see #writeGzippedObject(java.io.File, java.io.Serializable)
  */
public static void writeObject(File f, Serializable obj) {
    try {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
        oos.writeObject(obj);
        oos.close();
    } catch (IOException e) {
        System.err.println("Exception writing file " + f + ": " + e);
    }
}