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

/**
 * Get a stream that can be used for serialization
 * /* w w w  . j a v a2s  . c om*/
 * @param context
 *            We'll write our data to this context private dir
 * @param name
 * @return
 * @throws IOException
 */
public static ObjectOutputStream writeObjectStream(Context context, String name) throws IOException {
    OutputStream s = context.openFileOutput(name, Context.MODE_PRIVATE);

    // I'd prefer to not overwrite the old file, but Context doesn't offer a
    // fileRename option
    return new ObjectOutputStream(s);
}

From source file:com.comichentai.serialize.SerializeUtil.java

public static byte[] serialize(Object obj) {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;
    try {/*  w  ww .j av  a  2 s.  c o m*/
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        return baos.toByteArray();
    } catch (RuntimeException re) {
        throw re;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        closeQuietly(baos);
        closeQuietly(oos);
    }
}

From source file:com.mayalogy.mayu.io.LocalDataManager.java

public static void serializeAndWriteToFile(String outFile, Object o) throws IOException {
    FileOutputStream fout = new FileOutputStream(outFile + ".ser");
    ObjectOutputStream oos = new ObjectOutputStream(fout);
    oos.writeObject(o);//from   www .  ja v a2  s .c o m
    oos.close();
}

From source file:Main.java

public static void serialize(Object object, OutputStream outputStream) {
    try {//from  w w  w  . jav  a 2 s . c om
        final OutputStream buffer = new BufferedOutputStream(outputStream);
        final ObjectOutput output = new ObjectOutputStream(buffer);
        try {
            output.writeObject(object);
        } finally {
            output.close();
        }
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] toBytes(Object obj) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(obj);//from  w  ww .  j a  va  2 s.  c  o m
    byte[] arr = baos.toByteArray();
    return arr;
}

From source file:MainClass.java

public static void desEncrypt(String f1, String f2) throws Exception {
    SecretKey key = null;//from   w  w w  .  j a  v a 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:Test.java

private static void clientStart() {
    try {/*w  w w.  j a  va  2s  .c  o m*/
        InetSocketAddress hostAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 2583);
        AsynchronousSocketChannel clientSocketChannel = AsynchronousSocketChannel.open();
        Future<Void> connectFuture = clientSocketChannel.connect(hostAddress);
        connectFuture.get(); // Wait until connection is done.
        OutputStream os = Channels.newOutputStream(clientSocketChannel);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        for (int i = 0; i < 5; i++) {
            oos.writeObject("Look at me " + i);
            Thread.sleep(1000);
        }
        oos.writeObject("EOF");
        oos.close();
        clientSocketChannel.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:IO.serializer.java

public static void Serialize(Object object, String destinationFilename) {
    String serializedFilename = String.format("%s\\%s.%s", m_systemTempDirectory, destinationFilename,
            m_serializedFileExtension);//  www  .  j ava 2s .c  o  m

    try (FileOutputStream fos = new FileOutputStream(serializedFilename)) {
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(object);
        oos.close();
    } catch (IOException ex) {
        Console.PrintLine(
                String.format("Error serilizing object '%s': %s", destinationFilename, ex.getMessage()), true,
                false);
    }
}

From source file:Main.java

public static byte[] serialize(Object o) throws ObjectStreamException {
    ByteArrayOutputStream array = new ByteArrayOutputStream();
    ObjectOutputStream out = null;
    try {/* w w  w . j a  v  a 2s . com*/
        out = new ObjectOutputStream(array);
        out.writeObject(o);
        out.close();
    } catch (ObjectStreamException e) {
        throw e;
    } catch (IOException e) {
        // no handle
    }
    return array.toByteArray();
}

From source file:Main.java

public static <T> List<T> deepCopy(List<T> src) {
    try {//from  w ww.j av  a 2  s  . 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;
    }
}