Example usage for java.io ObjectOutput writeObject

List of usage examples for java.io ObjectOutput writeObject

Introduction

In this page you can find the example usage for java.io ObjectOutput writeObject.

Prototype

public void writeObject(Object obj) throws IOException;

Source Link

Document

Write an object to the underlying storage or stream.

Usage

From source file:Main.java

public static synchronized void writeArrayListToFile(ArrayList<double[]> x, String filename) {
    //        double  []x  = {1,2,4};
    //        String filename = "data.dat";
    ObjectOutput out = null;

    try {// ww w  . jav  a  2 s  .  c om
        out = new ObjectOutputStream(new FileOutputStream(new File(filename)));
        out.writeObject(x);
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static byte[] toByteArray(Serializable serializable) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {//from   ww  w  .  ja va 2 s . c om
        out = new ObjectOutputStream(bos);
        out.writeObject(serializable);
        byte[] bytes = bos.toByteArray();
        return bytes;
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        try {
            out.close();
            bos.close();
        } catch (IOException e) {
            //can be ignored;
        }

    }
}

From source file:Main.java

public static byte[] serialize(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    byte[] bytes = null;
    try {//from  w  ww.  j  a  va 2s . c om
        out = new ObjectOutputStream(bos);
        out.writeObject(o);
        bytes = bos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
        try {
            bos.close();
        } catch (IOException ex) {
            // ignore close exception
        }
    }
    return bytes;
}

From source file:org.kuali.rice.core.api.util.ChecksumUtils.java

/**
* Creates a checksum for the given object which must be serializable.
*
* @param object the serializable object for which to calculate the checksum
 * /*  w w w  .  j  a  v a2  s  .c o m*/
* @return A checksum value for the object.
*/
public static String calculateChecksum(Object object) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(object);
    } catch (IOException e) {
        throw new RiceRuntimeException(e);
    } finally {
        try {
            out.close();
        } catch (IOException e) {
        }
    }
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        return new String(Base64.encodeBase64(md.digest(bos.toByteArray())), "UTF-8");
    } catch (GeneralSecurityException ex) {
        throw new RiceRuntimeException(ex);
    } catch (UnsupportedEncodingException ex) {
        throw new RiceRuntimeException(ex);
    }
}

From source file:net.ulno.jpunch.util.Util.java

/**
 * Serializes Object into byte array./*from w w  w. ja  v  a 2 s.c o m*/
 * @throws IOException 
 */
public static byte[] serializeObject(Object obj) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    ObjectOutput oo;
    oo = new ObjectOutputStream(os);
    oo.writeObject(obj);

    return os.toByteArray();
}

From source file:org.apache.stratos.cloud.controller.persist.Serializer.java

/**
 * Serialize a {@link org.apache.stratos.cloud.controller.runtime.FasterLookUpDataHolder} to a byte array.
 * @param serializableObj/*from  ww  w . j  a v a2  s. c  om*/
 * @return byte[] 
 * @throws IOException
 */
public static byte[] serializeToByteArray(FasterLookUpDataHolder serializableObj) throws IOException {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(serializableObj);

        return bos.toByteArray();

    } finally {
        if (out != null) {
            out.close();
        }
        bos.close();
    }

}

From source file:org.apache.stratos.cloud.controller.persist.Serializer.java

/**
* Serialize a {@link org.apache.stratos.cloud.controller.runtime.FasterLookUpDataHolder} to a byte array.
* @param topology//from w ww .j  ava2  s.  c  om
* @return byte[]
* @throws IOException
*/
public static byte[] serializeToByteArray(Topology topology) throws IOException {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(topology);

        return bos.toByteArray();

    } finally {
        if (out != null) {
            out.close();
        }
        bos.close();
    }

}

From source file:com.bah.applefox.main.plugins.pageranking.utilities.PRtoFile.java

public static boolean writeToFile(String[] args) {
    String fileName = args[16];//w  w  w  .j a  v  a  2s  . c om
    File f = new File(fileName);
    try {
        f.createNewFile();
        OutputStream file = new FileOutputStream(f);
        OutputStream buffer = new BufferedOutputStream(file);
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(createMap(args));
        out.flush();
        out.close();
    } catch (IOException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
        return false;
    } catch (AccumuloException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
        return false;
    } catch (AccumuloSecurityException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
        return false;
    } catch (TableNotFoundException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
        return false;
    }

    return true;

}

From source file:com.linkedin.pinot.common.utils.DataTableJavaSerDe.java

/**
 * Helper method to serialize an object using Java ser/de.
 *
 * @param object  to serialize/*from w  ww .java  2 s.  co  m*/
 * @return Serialized byte[] for the object.
 */
public static byte[] serializeJavaObject(@Nonnull Object object) {
    byte[] bytes;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;

    try {
        try {
            out = new ObjectOutputStream(bos);
            out.writeObject(object);
        } catch (IOException e) {
            LOGGER.error("Caught exception while serializing object for data table.", e);
            Utils.rethrowException(e);
        }

        bytes = bos.toByteArray();
    } finally {
        IOUtils.closeQuietly((Closeable) out);
        IOUtils.closeQuietly(bos);
    }

    return bytes;
}

From source file:com.abcseo.comments.dao.RepositoryTreapImpl.java

private static byte[] object2Bytes(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out;
    try {//from   ww  w  .  j  a  va  2s  .co m
        out = new ObjectOutputStream(bos);
        out.writeObject(o);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bos.toByteArray();
}