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:cam.cl.kilo.RESTBarcode.java

/**
 * Serialise an object as a Base 64 string.
 *
 * @param o The object to serialise/*  ww  w.j a v  a  2  s. co m*/
 * @return Base64-encoded string.
 * @throws IOException
 */
private static String toString(Serializable o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);
    oos.close();
    return Base64.encodeBase64String(baos.toByteArray());
}

From source file:llc.rockford.webcast.StringUtils.java

public static String encodeBase64BinaryFile(InputStream is) {
    String base64EncodedFile = "";
    try {//from w ww . j a v a2  s.c  o  m
        byte[] bytesFromFile = IOUtils.toByteArray(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream stream = new ObjectOutputStream(baos);
        stream.write(bytesFromFile);
        stream.flush();
        stream.close();
        base64EncodedFile = new String(Base64.encodeBase64(baos.toByteArray()));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return base64EncodedFile;
}

From source file:glluch.com.ontotaxoseeker.TestsGen.java

public static String save(Object o, String className) throws FileNotFoundException, IOException {

    if (StringUtils.isEmpty(className)) {
        Out.p("TestsGen.save have received an anonomous object");
        className = "anonimousClass";
    }//w w w  .  j  ava2 s .  com

    FileOutputStream fileOut = new FileOutputStream("resources/test/" + className + ".ser");
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(o);
    out.close();
    fileOut.close();
    System.out.printf(
            "Serialized data is saved in " + "resources/test/" + className + ".ser" + System.lineSeparator());

    return "resources/test/" + className + ".ser";
}

From source file:Main.java

public static byte[] toBinary(Serializable obj) {
    if (obj == null) {
        return null;
    }// w  ww .  j a  v a2s .  c  o m
    if (obj instanceof byte[]) {
        return (byte[]) obj;
    }
    try {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        try {
            return baos.toByteArray();
        } finally {
            oos.close();
        }
    } catch (Exception e) {
        String msg = "Failed to convert the object to binary: obj=" + obj;
        throw new IllegalStateException(msg, e);
    }
}

From source file:edu.tufts.vue.util.Encryption.java

private static synchronized Key getKey() {
    try {//from   w  w  w  . j ava 2  s  .  c o m
        if (key == null) {
            File keyFile = new File(
                    VueUtil.getDefaultUserFolder().getAbsolutePath() + File.separator + KEY_FILE);
            if (keyFile.exists()) {
                ObjectInputStream is = new ObjectInputStream(new FileInputStream(keyFile));
                key = (Key) is.readObject();
                is.close();
            } else {
                key = KeyGenerator.getInstance(algorithm).generateKey();
                ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(keyFile));
                os.writeObject(key);
                os.close();
            }
            return key;
        } else {
            return key;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static byte[] getBytes(Serializable obj) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bout);
    out.writeObject(obj);/*www  .  ja v a 2  s  .  co  m*/
    out.flush();
    byte[] bytes = bout.toByteArray();
    bout.close();
    out.close();
    return bytes;
}

From source file:com.cloudera.recordservice.hcatalog.common.HCatRSUtil.java

public static String serialize(Serializable obj) throws IOException {
    if (obj == null) {
        return "";
    }/*w  w w  .j  a  v a 2s  . com*/
    try {
        ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
        ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
        objStream.writeObject(obj);
        objStream.close();
        return encodeBytes(serialObj.toByteArray());
    } catch (Exception e) {
        throw new IOException("Serialization error: " + e.getMessage(), e);
    }
}

From source file:io.smartspaces.scheduling.quartz.orientdb.internal.util.SerialUtils.java

public static byte[] serialize(Calendar calendar) throws JobPersistenceException {
    // ToDO(keith): Serialize better than Java serialization.
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    try {/*  w ww .  java2s. c  o  m*/
        ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
        objectStream.writeObject(calendar);
        objectStream.close();
        return byteStream.toByteArray();
    } catch (IOException e) {
        throw new JobPersistenceException("Could not serialize Calendar.", e);
    }
}

From source file:Main.java

/**
 * Serialize any object into String/*w  w  w.jav  a  2s . c  om*/
 * @param object Object
 * @return String
 * @throws IOException If unable to access Stream
 */
public static String serialize(Object object) throws java.io.IOException {
    if (object == null)
        return null;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(
            new Base64OutputStream(byteArrayOutputStream, 0));
    objectOutputStream.writeObject(object);
    objectOutputStream.flush();
    objectOutputStream.close();
    return byteArrayOutputStream.toString();
}

From source file:org.terracotta.management.cli.rest.HttpServices.java

private static void save(String filename, Object o) throws IOException {
    File path = new File(System.getProperty("user.home"), ".tc/mgmt");
    File file = new File(path, filename);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
    try {/*from w  w w. j a v a 2 s. c o m*/
        objectOutputStream.writeObject(o);
    } finally {
        objectOutputStream.close();
    }
}