Example usage for java.io ObjectOutput close

List of usage examples for java.io ObjectOutput close

Introduction

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

Prototype

public void close() throws IOException;

Source Link

Document

Closes the stream.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object object = new JButton("push me");

    ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
    out.writeObject(object);/*ww  w.j av  a2 s .com*/
    out.close();

    // Serialize to a byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    out = new ObjectOutputStream(bos);
    out.writeObject(object);
    out.close();

    // Get the bytes of the serialized object
    byte[] buf = bos.toByteArray();
    System.out.println(new String(buf));
}

From source file:Main.java

public static byte[] serializeObject(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {/* ww  w .j a  v a 2s.  com*/
        ObjectOutput out = new ObjectOutputStream(bos);
        out.writeObject(o);
        out.close();

        byte[] buf = bos.toByteArray();

        return buf;
    } catch (IOException ioe) {
        //         Log.e("serializeObject", "error", ioe);

        return null;
    }
}

From source file:Main.java

public static byte[] serializeObject(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {//from  w w  w.j  a  v  a 2s . co  m
        ObjectOutput out = new ObjectOutputStream(bos);
        out.writeObject(o);
        out.close();

        byte[] buf = bos.toByteArray();

        return buf;
    } catch (IOException ioe) {
        Log.e("serializeObject", "error", ioe);

        return null;
    }
}

From source file:org.apache.hadoop.mapred.lib.instanceapi.SerializableUtil.java

public static void serializeObject(Configuration conf, String objectPropertyName, Object object)
        throws IOException {
    ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(outBytes);
    out.writeObject(object);/*from   www  .ja v a2  s .com*/
    out.close();
    byte[] base64 = Base64.encodeBase64(outBytes.toByteArray());
    conf.set(objectPropertyName, new String(base64));
}

From source file:org.codehaus.wadi.core.util.Utils.java

public static byte[] getContent(Externalizable object, Streamer streamer) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutput oo = streamer.getOutputStream(baos);
    object.writeExternal(oo);// www .  j ava  2  s.c o m
    oo.close();
    return baos.toByteArray();
}

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 {//from w  w w.  java 2  s  . co  m
        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 void serialize(Object object, OutputStream outputStream) {
    try {/*from   www  . ja v  a  2s  .co m*/
        final OutputStream buffer = new BufferedOutputStream(outputStream);
        final ObjectOutput output = new ObjectOutputStream(buffer);
        try {
            output.writeObject(object);
        } finally {
            output.close();
        }
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void saveObject(Context context, String fileName, Object obj) throws IOException {
    FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ObjectOutput out = null;
    try {/*  w w w.j a  v a 2  s  . c o m*/
        out = new ObjectOutputStream(fos);
        out.writeObject(obj);
        out.flush();
    } finally {
        out.close();
        fos.close();
    }

}

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

public static boolean writeToFile(String[] args) {
    String fileName = args[16];/*from  ww  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: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
 * /*  ww w  .  j a v  a  2 s.  co  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);
    }
}