List of usage examples for java.io ObjectOutputStream close
public void close() throws IOException
From source file:misc.TestUtils.java
@SuppressWarnings("unchecked") public static <T extends Serializable> T cloneSerializable(T obj) { ObjectOutputStream out = null; ObjectInputStream in = null;//w w w .java 2 s .c om try { ByteArrayOutputStream bout = new ByteArrayOutputStream(); out = new ObjectOutputStream(bout); out.writeObject(obj); out.close(); ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); in = new ObjectInputStream(bin); Object copy = in.readObject(); in.close(); return (T) copy; } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ignore) { } } return null; }
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./*from ww w . j ava 2 s . c om*/ */ 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:Main.java
public static byte[] getBytes(Serializable obj) { byte[] bytes = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = null; try {/*from ww w . j a v a 2s . co m*/ oos = new ObjectOutputStream(bos); oos.writeObject(obj); oos.flush(); bytes = bos.toByteArray(); } catch (IOException ex) { ex.printStackTrace(); } finally { if (oos != null) { try { oos.close(); bos.close(); bos.close(); } catch (Exception e) { e.printStackTrace(); } } } return bytes; }
From source file:com.facebook.infrastructure.utils.FBUtilities.java
public static byte[] serializeToStream(Object o) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] bytes = ArrayUtils.EMPTY_BYTE_ARRAY; try {/*from w ww . j ava2s. com*/ ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(o); oos.flush(); bytes = bos.toByteArray(); oos.close(); bos.close(); } catch (IOException e) { LogUtil.getLogger(FBUtilities.class.getName()).info(LogUtil.throwableToString(e)); } return bytes; }
From source file:net.sourceforge.jaulp.io.SerializedObjectUtils.java
/** * Writes the given object to the given File. * * @param obj/*w w w .jav a 2 s . c om*/ * The object to write to the File. * @param file * In this file will be the object saved. * @return true if the object was written to the file otherwise false. * @throws IOException * Signals that an I/O exception has occurred. */ public static boolean writeSerializedObjectToFile(final Object obj, final File file) throws IOException { boolean written = true; FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(file); oos = new ObjectOutputStream(fos); oos.writeObject(obj); oos.flush(); oos.close(); } finally { StreamUtils.closeOutputStream(oos); StreamUtils.closeOutputStream(fos); } return written; }
From source file:com.ligadata.EncryptUtils.EncryptionUtil.java
/** * Generate key which contains a pair of private and public key using 1024 * bytes. Store the set of keys in given files publicKeyFile,privateKeyFile * @param algorithm/* w ww . j a v a 2 s . co m*/ * : algorithm used * @param publicKeyFile * :The file containing public key * @param privateKeyFile * :The file containing private key */ public static void generateSampleKeys(String algorithm, String publicKeyFile, String privateKeyFile) { try { if (areKeysPresent(publicKeyFile, privateKeyFile)) { return; } final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm); keyGen.initialize(1024); final KeyPair key = keyGen.generateKeyPair(); File privateKeyFD = new File(privateKeyFile); File publicKeyFD = new File(publicKeyFile); // Create files to store public and private key if (privateKeyFD.getParentFile() != null) { privateKeyFD.getParentFile().mkdirs(); } privateKeyFD.createNewFile(); if (publicKeyFD.getParentFile() != null) { publicKeyFD.getParentFile().mkdirs(); } publicKeyFD.createNewFile(); // Saving the Public key in a file ObjectOutputStream publicKeyOS = new ObjectOutputStream(new FileOutputStream(publicKeyFD)); publicKeyOS.writeObject(key.getPublic()); publicKeyOS.close(); // Saving the Private key in a file ObjectOutputStream privateKeyOS = new ObjectOutputStream(new FileOutputStream(privateKeyFD)); privateKeyOS.writeObject(key.getPrivate()); privateKeyOS.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:de.alpharogroup.io.SerializedObjectExtensions.java
/** * Writes the given object to the given File. * * @param obj//w w w .j a v a 2 s. c o m * The object to write to the File. * @param file * In this file will be the object saved. * @return true if the object was written to the file otherwise false. * @throws IOException * Signals that an I/O exception has occurred. */ public static boolean writeSerializedObjectToFile(final Object obj, final File file) throws IOException { final boolean written = true; FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(file); oos = new ObjectOutputStream(fos); oos.writeObject(obj); oos.flush(); oos.close(); } finally { StreamExtensions.closeOutputStream(oos); StreamExtensions.closeOutputStream(fos); } return written; }
From source file:de.alpharogroup.io.SerializedObjectUtils.java
/** * Writes the given object to the given File. * * @param obj//ww w . jav a 2 s. c o m * The object to write to the File. * @param file * In this file will be the object saved. * @return true if the object was written to the file otherwise false. * @throws IOException * Signals that an I/O exception has occurred. */ public static boolean writeSerializedObjectToFile(final Object obj, final File file) throws IOException { final boolean written = true; FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(file); oos = new ObjectOutputStream(fos); oos.writeObject(obj); oos.flush(); oos.close(); } finally { StreamUtils.closeOutputStream(oos); StreamUtils.closeOutputStream(fos); } return written; }
From source file:com.couchbase.client.java.transcoder.TranscoderUtils.java
/** * Serializes the input into a ByteBuf.// w ww. j a va 2 s.c o m * * @param serializable the object to serialize. * @return the serialized object. * @throws Exception if something goes wrong during serialization. */ public static ByteBuf serialize(final Serializable serializable) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ; ObjectOutputStream os = new ObjectOutputStream(bos); os.writeObject(serializable); byte[] serialized = bos.toByteArray(); os.close(); bos.close(); return Unpooled.buffer().writeBytes(serialized); }
From source file:Main.java
public static String objectSerializableToString(Serializable object) { ByteArrayOutputStream baos = null; ObjectOutputStream os = null; try {//from w w w. j av a 2 s .c o m if (object != null) { baos = new ByteArrayOutputStream(); os = new ObjectOutputStream(baos); os.writeObject(object); os.flush(); baos.flush(); return baos.toString("UTF-8"); } } catch (Throwable e) { } finally { try { if (os != null) { os.close(); } } catch (Throwable e2) { } try { if (baos != null) { baos.close(); } } catch (Throwable e2) { } } return null; }