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:com.mnt.base.util.SerializeUtil.java

public static byte[] serialize(Object obj) {
    byte[] result;

    if (obj == null) {
        result = null;/*from  w  ww . ja  v  a  2s .  c o  m*/
    } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);

        } catch (IOException e) {
            log.error("error while open byte array output stream.", e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    log.error("error while close byte array output stream.", e);
                }
            }
        }

        result = baos.toByteArray();
    }

    return result;
}

From source file:de.zib.gndms.gritserv.delegation.DelegationAux.java

public static NormalizedString encodeDelegationEPR(EndpointReferenceType epr)
        throws IOException, SerializationException {

    ByteArrayOutputStream bse = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bse);
    oos.writeObject(ObjectSerializer.toString(epr, QNAME));

    final Base64 b64 = new Base64(4000, new byte[] {}, true);
    final String uuepr = b64.encodeToString(bse.toByteArray());
    return new NormalizedString(uuepr);
}

From source file:it.unimi.di.big.mg4j.io.IOFactories.java

public static void storeObject(final IOFactory ioFactory, final Object object, final String filename)
        throws IOException {
    final ObjectOutputStream oos = new ObjectOutputStream(
            new FastBufferedOutputStream(ioFactory.getOutputStream(filename)));
    oos.writeObject(object);
    oos.close();/*from ww  w  .  j a v a 2  s.  c  o  m*/
}

From source file:SerialIntList.java

/**
 * Use object serialization to make a "deep clone" of the object o. This
 * method serializes o and all objects it refers to, and then deserializes
 * that graph of objects, which means that everything is copied. This differs
 * from the clone() method of an object which is usually implemented to
 * produce a "shallow" clone that copies references to other objects, instead
 * of copying all referenced objects.//  w  w  w.  j a  v  a2s. c o m
 */
static Object deepclone(final Serializable o) throws IOException, ClassNotFoundException {
    // Create a connected pair of "piped" streams.
    // We'll write bytes to one, and them from the other one.
    final PipedOutputStream pipeout = new PipedOutputStream();
    PipedInputStream pipein = new PipedInputStream(pipeout);

    // Now define an independent thread to serialize the object and write
    // its bytes to the PipedOutputStream
    Thread writer = new Thread() {
        public void run() {
            ObjectOutputStream out = null;
            try {
                out = new ObjectOutputStream(pipeout);
                out.writeObject(o);
            } catch (IOException e) {
            } finally {
                try {
                    out.close();
                } catch (Exception e) {
                }
            }
        }
    };
    writer.start(); // Make the thread start serializing and writing

    // Meanwhile, in this thread, read and deserialize from the piped
    // input stream. The resulting object is a deep clone of the original.
    ObjectInputStream in = new ObjectInputStream(pipein);
    return in.readObject();
}

From source file:com.ikon.util.Serializer.java

/**
 * @param obj/* w ww .ja va 2  s  .  c om*/
 */
public static void write(String filename, Object obj) {
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;

    try {
        fos = new FileOutputStream(Config.HOME_DIR + File.separator + filename + ".ser");
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
    } catch (FileNotFoundException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    } finally {
        IOUtils.closeQuietly(oos);
        IOUtils.closeQuietly(fos);
    }
}

From source file:de.tud.cs.se.flashcards.persistence.Store.java

public static void saveSeries(FlashcardSeries series, File file) throws IOException {

    ObjectOutputStream oout = null;
    try {/*from   w w w . ja v a2  s .  c o m*/
        oout = new ObjectOutputStream(new FileOutputStream(file));
        oout.writeInt(series.getSize());
        for (int i = series.getSize() - 1; i >= 0; i -= 1) {
            oout.writeObject(series.getElementAt(i));
        }
    } finally {
        if (oout != null)
            oout.close();
    }

}

From source file:sawtooth.examples.jvmsc.JvmScHandler.java

/**
 * the method that returnsa byte[] from an object.
 *//*from  w ww .j  a  v  a 2  s  .  co m*/
public static byte[] dataToByteArray(Object data) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream input = new ByteArrayOutputStream();
    ObjectOutputStream output = new ObjectOutputStream(input);
    output.writeObject(data);
    return input.toByteArray();
}

From source file:com.pongasoft.util.io.IOUtils.java

public static <T> void serialize(T object, OutputStream outputStream) throws IOException {
    ObjectOutputStream oos = new ObjectOutputStream(outputStream);
    oos.writeObject(object);
    oos.flush();//from w  w  w.  jav a  2  s . c o  m
}

From source file:com.wabacus.util.DesEncryptTools.java

public static boolean createEncryptKey(File file) {
    SecretKey deskey = null;/*from www .java 2  s. co m*/
    try {
        KeyGenerator keygen = KeyGenerator.getInstance("DESede");
        deskey = keygen.generateKey();
    } catch (NoSuchAlgorithmException e) {
        throw new WabacusConfigLoadingException("?", e);
    }
    if (deskey == null)
        return false;
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(deskey);
    } catch (FileNotFoundException e) {
        throw new WabacusConfigLoadingException(
                "?" + file.getPath(), e);
    } catch (IOException e) {
        throw new WabacusConfigLoadingException(
                "?" + file.getPath() + "", e);
    } finally {
        try {
            if (fos != null)
                fos.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        try {
            if (oos != null)
                oos.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    return true;
}

From source file:net.big_oh.common.web.WebUtil.java

private static int approximateObjectSize(Object obj) throws IOException {
    java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
    java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos);

    try {//  w  w w  . j  a v a2s  . c om

        int sizeBefore = baos.size();
        oos.writeObject(obj);
        logger.debug("Approximated size of a " + obj.getClass().getName() + " object as "
                + (baos.size() - sizeBefore) + " bytes.");

        return baos.size();

    } catch (NotSerializableException e) {
        logger.warn(
                "Failed to approximate size because of a non-serializable, non-transient object in the object graph for "
                        + obj.getClass().getName(),
                e);
        return 0;
    } finally {
        IOUtils.closeQuietly(baos);
        IOUtils.closeQuietly(oos);
    }

}