Example usage for java.io ObjectOutputStream close

List of usage examples for java.io ObjectOutputStream close

Introduction

In this page you can find the example usage for java.io ObjectOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes the stream.

Usage

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 {/*  w w  w  .j a v a2s  .  c om*/
        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.clustercontrol.plugin.impl.AsyncTask.java

/**
 * SerializableBinary???/*from  www.  j  a  va2  s .co m*/
 * @param obj Serializable
 * @return ??Binary
 * @throws IOException
 */
public static byte[] encodeBinary(Serializable obj) throws IOException {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;
    byte[] bytes = null;

    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        bytes = baos.toByteArray();
    } finally {
        if (oos != null) {
            oos.close();
        }
        if (baos != null) {
            baos.close();
        }
    }

    return bytes;
}

From source file:edu.usc.pgroup.floe.utils.Utils.java

/**
 * Serializer used for storing data into zookeeper. We use the default
 * java serializer (since the amount of data to be serialized is usually
 * very small).//from  ww  w  .  jav a 2s  .  c o  m
 * NOTE: THIS IS DIFFERENT FROM THE SERIALIZER USED DURING COMMUNICATION
 * BETWEEN DIFFERENT FLAKES. THAT ONE IS PLUGABBLE, THIS IS NOT.
 *
 * @param obj Object to be serialized
 * @return serialized byte array.
 */
public static byte[] serialize(final Object obj) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.close();
        return bos.toByteArray();
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

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

/**
 * Serialize to a byte array// w  w  w. j a  v  a2s .c om
 * @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:net.sf.taverna.t2.activities.wsdlsir.views.WSDLActivityConfigurationView.java

/**
 * Write the object to a Base64 string// w  w  w.ja  v a 2s.c o  m
 * TODO: mover a algun sitio comun?
 * @param o
 * @return
 * @throws IOException
 */
private static String serializetoString(Serializable o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);
    oos.close();
    return new String(Base64.encode(baos.toByteArray()));
}

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

private static byte[] serialize(Object state) {
    ObjectOutputStream oos = null;
    try {/*from   w  w w. j  a v a  2s.  co  m*/
        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:de.pawlidi.openaletheia.generator.KeyGenerator.java

private static boolean writeKeySpec(File file, BigInteger mod, BigInteger exp) {
    boolean successful = false;
    ObjectOutputStream outputStream = null;
    try {// w w w. jav a2  s  .  co 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:com.dev.pygmy.util.Utils.java

/**
 * Saves the current game by serializing it and storing in locally
 * on the device/* w  ww.j a v a2 s . co m*/
 */
public static void saveGame(PygmyGame game, String path) {
    ObjectOutputStream oos = null;
    try {
        File history = new File(path);
        history.getParentFile().createNewFile();
        FileOutputStream fout = new FileOutputStream(history);
        oos = new ObjectOutputStream(fout);
        oos.writeObject(game);
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (oos != null) {
                oos.flush();
                oos.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.beetle.framework.util.ObjectUtil.java

/**
 * ??/*from   w  ww . j  ava2 s. c o  m*/
 * 
 * @param obj
 * @return
 */
public final static byte[] objToBytes(Object obj) {
    ByteArrayOutputStream bao = null;
    ObjectOutputStream oos;
    try {
        bao = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bao);
        oos.writeObject(obj);
        oos.flush();
        oos.close();
        return bao.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (bao != null) {
                bao.close();
                bao = null;
            }
        } catch (IOException e) {
        }
    }
}

From source file:com.beetle.framework.util.ObjectUtil.java

/**
 * ???//from w ww.  j  av  a  2s .  c  om
 * 
 * @param obj
 * @return
 */
public final static int sizeOf(Object obj) {
    ByteArrayOutputStream bao = null;
    ObjectOutputStream oos;
    try {
        bao = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bao);
        oos.writeObject(obj);
        oos.flush();
        oos.close();
        return bao.size();
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    } finally {
        try {
            if (bao != null) {
                bao.close();
                bao = null;
            }
        } catch (IOException e) {
        }
    }
}