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:test.integ.be.fedict.performance.util.PerformanceResultDialog.java

public static void writeResults(PerformanceResultsData data) throws Exception {

    DateTime dt = new DateTime();
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd_MM_yyyy_HHmmss");
    File resultsFile = new File("performance_results_" + fmt.print(dt) + ".data");
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(resultsFile));
    out.writeObject(data);
}

From source file:SerializationUtils.java

/**
 * <p>Serializes an <code>Object</code> to the specified stream.</p>
 *
 * <p>The stream will be closed once the object is written.
 * This avoids the need for a finally clause, and maybe also exception
 * handling, in the application code.</p>
 * //from  ww w  . j  ava2s.c om
 * <p>The stream passed in is not buffered internally within this method.
 * This is the responsibility of your application if desired.</p>
 *
 * @param obj  the object to serialize to bytes, may be null
 * @param outputStream  the stream to write to, must not be null
 * @throws IllegalArgumentException if <code>outputStream</code> is <code>null</code>
 * @throws SerializationException (runtime) if the serialization fails
 */
public static void serialize(Serializable obj, OutputStream outputStream) {
    if (outputStream == null) {
        throw new IllegalArgumentException("The OutputStream must not be null");
    }
    ObjectOutputStream out = null;
    try {
        // stream closed in the finally
        out = new ObjectOutputStream(outputStream);
        out.writeObject(obj);

    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
}

From source file:jp.xet.baseunits.tests.SerializationTester.java

/**
 * ????????/* ww  w. j a va  2s  . c  o  m*/
 * 
 * @param serializable 
 * @throws AssertionError ????
 */
public static void assertCanBeSerialized(Object serializable) {
    if (Serializable.class.isInstance(serializable) == false) {
        fail("Object doesn't implement java.io.Serializable interface: " + serializable.getClass());
    }

    ObjectOutputStream out = null;
    ObjectInputStream in = null;
    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
    ByteArrayInputStream byteArrayIn = null;
    try {
        out = new ObjectOutputStream(byteArrayOut);
        out.writeObject(serializable);

        byteArrayIn = new ByteArrayInputStream(byteArrayOut.toByteArray());
        in = new ObjectInputStream(byteArrayIn);
        Object deserialized = in.readObject();
        if (serializable.equals(deserialized) == false) {
            fail("Reconstituted object is expected to be equal to serialized");
        }
    } catch (IOException e) {
        fail(e.getMessage());
    } catch (ClassNotFoundException e) {
        fail(e.getMessage());
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
    }
}

From source file:com.google.caja.precajole.StaticPrecajoleMap.java

private static byte[] serialize(Object obj) {
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    try {//  w ww .j  a  va2s.  co  m
        ObjectOutputStream ostr = new ObjectOutputStream(buf);
        ostr.writeObject(obj);
        ostr.close();
    } catch (IOException e) {
        throw new SomethingWidgyHappenedError(e);
    }
    return buf.toByteArray();
}

From source file:br.com.ufjf.labredes.crypto.Cryptography.java

public static void geraChave() {
    try {//from   w w  w  .  ja  va 2 s .  co m

        final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM_ASYM);
        keyGen.initialize(1024);
        final KeyPair key = keyGen.generateKeyPair();

        File chavePrivadaFileServer = new File(path, PATH_CHAVE_PRIVADA_SERVER);
        File chavePublicaFileServer = new File(path, PATH_CHAVE_PUBLICA_SERVER);

        // Cria os arquivos para armazenar a chave Privada e a chave Publica            
        if (chavePrivadaFileServer.getParentFile() != null) {
            chavePrivadaFileServer.getParentFile().mkdirs();
        }

        chavePrivadaFileServer.createNewFile();

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

        chavePublicaFileServer.createNewFile();

        // Salva a Chave Pblica do servidor no arquivo
        ObjectOutputStream chavePublicaOSS = new ObjectOutputStream(
                new FileOutputStream(chavePublicaFileServer));
        chavePublicaOSS.writeObject(key.getPublic());
        chavePublicaOSS.close();

        // Salva a Chave Privada do servidor no arquivo
        ObjectOutputStream chavePrivadaOSS = new ObjectOutputStream(
                new FileOutputStream(chavePrivadaFileServer));
        chavePrivadaOSS.writeObject(key.getPrivate());
        chavePrivadaOSS.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:fr.eo.util.dumper.Dumper.java

private static void writeDescriptorFile(DumpFilesDescriptor descriptor) {
    String path = assetFolder + "/" + Const.DUMP_FILE_DESCRIPTOR_NAME;
    ObjectOutputStream oos = null;
    try {/*  w w  w.  j  av  a2  s  . c  om*/
        oos = new ObjectOutputStream(new FileOutputStream(new File(path)));
        oos.writeObject(descriptor);
        oos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:net.myrrix.common.io.IOUtils.java

/**
 * Serializes an object, with gzip compression, to a given file.
 *///  ww w  .j  ava  2 s. com
public static <T extends Serializable> void writeObjectToFile(File f, T t) throws IOException {
    Preconditions.checkArgument(f.getName().endsWith(".gz"), "File should end in .gz: %s", f);
    ObjectOutputStream out = new ObjectOutputStream(buildGZIPOutputStream(f));
    try {
        out.writeObject(t);
    } finally {
        out.close();
    }
}

From source file:net.menthor.editor.v2.util.Util.java

/** Write the object to a Base64 string. */
public static String toBase64String(Serializable o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);
    oos.close();/*from www .j  a v a2s .c  om*/
    return Base64.getEncoder().encodeToString(baos.toByteArray());
}

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 {//ww  w .j av  a2 s  . c o 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.facebook.infrastructure.utils.FBUtilities.java

public static byte[] serializeToStream(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] bytes = ArrayUtils.EMPTY_BYTE_ARRAY;
    try {//from  w w w .  j av a2 s.c o m
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(o);
        oos.flush();
        bytes = bos.toByteArray();
        oos.close();
        bos.close();
    } catch (IOException e) {
        LogUtil.getLogger(FBUtilities.class.getName()).info(LogUtil.throwableToString(e));
    }
    return bytes;
}