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:BytesUtil.java

public static byte[] toByteArray(Object obj) throws IOException {
    byte[] bytes = null;
    ByteArrayOutputStream bos = null;
    ObjectOutputStream oos = null;
    try {//w  ww  .j  av  a 2 s .c  o m
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        bytes = bos.toByteArray();
    } finally {
        if (oos != null) {
            oos.close();
        }
        if (bos != null) {
            bos.close();
        }
    }
    return bytes;
}

From source file:SerialIntList.java

/**
 * Serialize the object o (and any Serializable objects it refers to) and
 * store its serialized state in File f.
 *///from   w  w w  .j  av  a  2  s .c o m
static void store(Serializable o, File f) throws IOException {
    ObjectOutputStream out = // The class for serialization
            new ObjectOutputStream(new FileOutputStream(f));
    out.writeObject(o); // This method serializes an object graph
    out.close();
}

From source file:Main.java

public static void writeObjectToFile(Object object, File file) {
    ObjectOutputStream oos = null;
    try {/*ww  w.j av  a 2 s .  c  o  m*/
        FileOutputStream fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(object);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:bencoding.securely.Converters.java

public static String serializeObjectToString(Object object) throws Exception {

    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(arrayOutputStream);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOutputStream);

    objectOutputStream.writeObject(object);

    objectOutputStream.flush();/*from   w w  w .  j ava 2  s  .c  o m*/
    objectOutputStream.close();
    gzipOutputStream.close();
    arrayOutputStream.close();

    String objectString = new String(Base64.encodeToString(arrayOutputStream.toByteArray(), Base64.DEFAULT));

    return objectString;
}

From source file:com.google.caja.precajole.StaticPrecajoleMap.java

private static byte[] serialize(Object obj) {
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    try {// www  .  ja v a  2s.c om
        ObjectOutputStream ostr = new ObjectOutputStream(buf);
        ostr.writeObject(obj);
        ostr.close();
    } catch (IOException e) {
        throw new SomethingWidgyHappenedError(e);
    }
    return buf.toByteArray();
}

From source file:Main.java

public static void saveSerializableObjectToFile(Object object, FileOutputStream fileOut) {
    ObjectOutputStream out = null;
    try {//from w w w .  j  a v a  2 s .co  m
        out = new ObjectOutputStream(fileOut);
        out.writeObject(object);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:net.menthor.editor.v2.util.Util.java

/** Write the object to a Base64 string. */
public static String toBase64String(Serializable o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);//from  w w  w  .j a  v a2s  .  c o m
    oos.close();
    return Base64.getEncoder().encodeToString(baos.toByteArray());
}

From source file:Main.java

/**
 * Serialize an object.//from w w w .j  av  a  2  s . c o m
 * @param obj a serializable object.
 * @return a byte array of the serialized object or null if an error occurs.
 */
public static byte[] serialize(Object obj) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.flush();
        baos.flush();
        return baos.toByteArray();
    } catch (IOException e) {
        return null;
    } finally {
        try {
            if (oos != null)
                oos.close();
        } catch (IOException e) {
        }
    }
}

From source file:Main.java

static byte[] toBlob(Serializable object) throws IOException {
    ObjectOutputStream oos = null;
    ByteArrayOutputStream bos;//from  w w  w .  j  ava  2s.  co m
    byte[] result = null;
    try {
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(object);
        result = bos.toByteArray();
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:br.com.ufjf.labredes.crypto.Cryptography.java

public static void geraChave() {
    try {//from w  w w  . jav  a2s  .  c  om

        final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM_ASYM);
        keyGen.initialize(1024);
        final KeyPair key = keyGen.generateKeyPair();

        File chavePrivadaFileServer = new File(path, PATH_CHAVE_PRIVADA_SERVER);
        File chavePublicaFileServer = new File(path, PATH_CHAVE_PUBLICA_SERVER);

        // Cria os arquivos para armazenar a chave Privada e a chave Publica            
        if (chavePrivadaFileServer.getParentFile() != null) {
            chavePrivadaFileServer.getParentFile().mkdirs();
        }

        chavePrivadaFileServer.createNewFile();

        if (chavePublicaFileServer.getParentFile() != null) {
            chavePublicaFileServer.getParentFile().mkdirs();
        }

        chavePublicaFileServer.createNewFile();

        // Salva a Chave Pblica do servidor no arquivo
        ObjectOutputStream chavePublicaOSS = new ObjectOutputStream(
                new FileOutputStream(chavePublicaFileServer));
        chavePublicaOSS.writeObject(key.getPublic());
        chavePublicaOSS.close();

        // Salva a Chave Privada do servidor no arquivo
        ObjectOutputStream chavePrivadaOSS = new ObjectOutputStream(
                new FileOutputStream(chavePrivadaFileServer));
        chavePrivadaOSS.writeObject(key.getPrivate());
        chavePrivadaOSS.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}