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:de.pawlidi.openaletheia.generator.KeyGenerator.java

private static boolean writeKeySpec(File file, BigInteger mod, BigInteger exp) {
    boolean successful = false;
    ObjectOutputStream outputStream = null;
    try {//from ww  w.  j  a  v a  2 s  . c o  m
        outputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
        try {
            outputStream.writeObject(mod);
            outputStream.writeObject(exp);
            successful = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                outputStream.close();
                outputStream = null;
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return successful;
}

From source file:org.socialhistoryservices.security.MongoTokenStore.java

private static byte[] serialize(Object state) {
    ObjectOutputStream oos = null;
    try {/*from  www. ja va  2  s.  com*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
        oos = new ObjectOutputStream(bos);
        oos.writeObject(state);
        oos.flush();
        return bos.toByteArray();
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
                // eat it
            }
        }
    }
}

From source file:com.github.fcannizzaro.prefs.Prefs.java

/**
 * Save Object (Serializable) Value//from  w  w  w  . ja v a  2 s  .c o  m
 *
 * @param key   of value
 * @param value to store
 */
public static <T extends Serializable> void putObject(String key, T value) {
    String outputName = "res/" + key.toLowerCase() + ".data";
    try {
        FileOutputStream os = new FileOutputStream(outputName);
        ObjectOutputStream object = new ObjectOutputStream(os);
        object.writeObject(value);
        objects.put(key, outputName);
        updateMemory();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.canoo.webtest.util.FileUtil.java

/**
 * Helper method for writing objects to a file.
 *
 * @param file the file to write to//from  w w  w  .  j a va 2s  .  c om
 * @param object the object to write
 * @param step step requesting the operation
 */
public static boolean tryWriteObjectToFile(final File file, final Object object, final Step step) {
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    String message = "creating";
    boolean success = false;
    try {
        fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        message = "filling";
        oos.writeObject(object);
        success = true;
    } catch (IOException e) {
        LOG.error("Error during write: " + e.getMessage(), e);
        if (step != null) {
            throw new StepExecutionException("Error " + message + " file: " + e.getMessage(), step);
        }
    } finally {
        StreamBoundary.closeOutputStream(oos);
        StreamBoundary.closeOutputStream(fos);
    }
    return success;
}

From source file:edu.iu.daal_cov.COVDaalCollectiveMapper.java

private static ByteArray serializePartialResult(PartialResult partialResult) throws IOException {
    /* Create an output stream to serialize the numeric table */
    ByteArrayOutputStream outputByteStream = new ByteArrayOutputStream();
    ObjectOutputStream outputStream = new ObjectOutputStream(outputByteStream);

    /* Serialize the numeric table into the output stream */
    partialResult.pack();/*from w  w  w. j  av a  2s  .  c  o m*/
    outputStream.writeObject(partialResult);

    /* Store the serialized data in an array */
    byte[] serializedPartialResult = outputByteStream.toByteArray();

    ByteArray partialResultHarp = new ByteArray(serializedPartialResult, 0, serializedPartialResult.length);
    return partialResultHarp;
}

From source file:com.griddynamics.jagger.util.SerializationUtils.java

public static String toString(Serializable o) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {/*w w  w.j  a  va 2  s .  co  m*/
        if (useJBoss) {
            oos = new JBossObjectOutputStream(baos);
        } else {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
        }
        oos.writeObject(o);
    } catch (IOException e) {
        log.error("Serialization exception ", e);
        throw new TechnicalException(e);
    } finally {
        String s = new String(Base64Coder.encode(baos.toByteArray()));
        if (s.isEmpty()) {
            log.info("toString({}, '{}', '{}')", toStringCount.getAndIncrement(), s, o);
        }
        try {
            Closeables.close(oos, true);
        } catch (IOException e) {
            log.warn("IOException should not have been thrown.", e);
        }
        return s;
    }
}

From source file:com.jaspersoft.jasperserver.util.JasperSerializationUtil.java

/**
 * Serialize to a byte array// ww  w  .  jav a 2  s. c o  m
 * @param input
 * @return
 */
public static byte[] serialize(Object input) {

    byte[] output = null;
    Exception exp = null;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    long startTime = System.currentTimeMillis();
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Enter serialize .. Start Time" + System.currentTimeMillis());
        }

        oos = new ObjectOutputStream(bos);
        oos.writeObject(input);
        output = bos.toByteArray();
    } catch (IOException e) {
        exp = e;
    } finally {
        try {
            if (null != oos) {
                oos.close();
            }
            bos.close();
        } catch (IOException e) {
        }
        if (logger.isDebugEnabled()) {
            long elapsedTime = System.currentTimeMillis() - startTime;
            logger.debug("Exit serialize .. Total Time Spent: " + elapsedTime);
        }

        if (null != exp) {
            throw new RuntimeException(exp);
        }
    }

    return output;
}

From source file:de.tudarmstadt.ukp.dkpro.tc.svmhmm.util.SVMHMMUtils.java

/**
 * Saves label-integer mapping to a file
 *
 * @param mapping    mapping/*w ww . ja  v a2  s.c  o m*/
 * @param outputFile file
 * @throws IOException
 */
public static void saveMapping(BidiMap mapping, File outputFile) throws IOException {
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(outputFile));
    objectOutputStream.writeObject(mapping);

    IOUtils.closeQuietly(objectOutputStream);
}

From source file:com.titilink.camel.rest.util.CommonUtils.java

/**
 * clone ? ?//  w  w  w .j  a v  a  2  s  .c  o  m
 *
 * @param obj ?
 * @return ?
 */
public static Object clone(Object obj) {
    if (obj == null) {
        return null;
    }

    Object anotherObj = null;
    byte[] bytes = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        bytes = baos.toByteArray();
    } catch (Exception ex) {
        LOGGER.error("CloneObjectUtil cloneexception ", ex);
        return null;
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (Exception e) {
                LOGGER.error("CloneObjectUtil cloneexception ", e);
            }
        }
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(bais);
        anotherObj = ois.readObject();
    } catch (Exception ex) {
        LOGGER.error("CloneObjectUtil cloneexception ", ex);
        return null;
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (Exception e) {
                LOGGER.error("CloneObjectUtil cloneexception ", e);
            }
        }
    }
    return anotherObj;
}

From source file:android.databinding.tool.util.GenerationalClassUtil.java

public static void writeIntermediateFile(ProcessingEnvironment processingEnv, String packageName,
        String fileName, Serializable object) {
    ObjectOutputStream oos = null;
    try {/*from   ww  w.  ja  va2  s.c om*/
        FileObject intermediate = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT,
                packageName, fileName);
        OutputStream ios = intermediate.openOutputStream();
        oos = new ObjectOutputStream(ios);
        oos.writeObject(object);
        oos.close();
        L.d("wrote intermediate bindable file %s %s", packageName, fileName);
    } catch (IOException e) {
        L.e(e, "Could not write to intermediate file: %s", fileName);
    } finally {
        IOUtils.closeQuietly(oos);
    }
}