List of usage examples for java.io ObjectOutputStream flush
public void flush() throws IOException
From source file:org.kuali.student.common.spring.SpringProxyTestUtils.java
public static Object testBeanSerialization(Object bean, long expectedSize) throws IOException, ClassNotFoundException { File tempFile = File.createTempFile("proxy", "dat"); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tempFile)); oos.writeObject(bean);// w w w . j av a2 s . c o m oos.flush(); oos.close(); // check the size of that file long serializedSizeInBytes = FileUtils.sizeOf(tempFile); Assert.assertEquals(expectedSize, serializedSizeInBytes); ObjectInputStream iis = new ObjectInputStream(new FileInputStream(tempFile)); Object deserialized = iis.readObject(); iis.close(); return deserialized; }
From source file:org.kuali.student.common.spring.SpringProxyTestUtils.java
public static Object testBeanSerialization(Object bean, String expectedToStringValue, long expectedSize) throws IOException, ClassNotFoundException { File tempFile = File.createTempFile("proxy", "dat"); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tempFile)); oos.writeObject(bean);//from w w w . jav a2s .c o m oos.flush(); oos.close(); // check the size of that file long serializedSizeInBytes = FileUtils.sizeOf(tempFile); Assert.assertEquals(expectedSize, serializedSizeInBytes); ObjectInputStream iis = new ObjectInputStream(new FileInputStream(tempFile)); Object generated = iis.readObject(); iis.close(); Assert.assertEquals(expectedToStringValue, generated.toString()); return generated; }
From source file:com.dragome.callbackevictor.serverside.utils.ReflectionUtils.java
public static Object cast(Object o) throws IOException, ClassNotFoundException { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(buffer); oos.writeObject(o);// w w w .ja v a 2 s.c o m oos.flush(); final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); return ois.readObject(); }
From source file:com.db4o.sync4o.SyncKey.java
static public String toEncodedString(SyncKey key) throws Exception { ByteArrayOutputStream bs = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(bs); os.writeObject(key);//from w ww . j a v a 2 s. co m os.flush(); os.close(); return new String(Base64.encodeBase64(bs.toByteArray())); }
From source file:de.tudarmstadt.ukp.dkpro.core.frequency.tfidf.util.TfidfUtils.java
public static void serialize(Object object, String fileName) throws Exception { File file = new File(fileName); if (!file.exists()) FileUtils.touch(file);/* w w w .j a v a 2s . c o m*/ if (file.isDirectory()) { throw new IOException("A directory with that name exists!"); } ObjectOutputStream objOut; objOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file))); objOut.writeObject(object); objOut.flush(); objOut.close(); }
From source file:it.restrung.rest.utils.IOUtils.java
/** * Serializes ans saves a Serializable object to a file * * @param object the source object/*w w w.ja va 2 s . co m*/ * @param file the target file */ static public void saveSerializableObjectToDisk(Object object, File file) { try { file.delete(); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); GZIPOutputStream gzos = new GZIPOutputStream(fos); ObjectOutputStream out = new ObjectOutputStream(gzos); out.writeObject(object); out.flush(); out.close(); } catch (FileNotFoundException e) { Log.d(IOUtils.class.getName(), "Error, file not found for save serializable object to disk"); throw new RuntimeException(e); } catch (IOException e) { Log.d(IOUtils.class.getName(), "Error on save serializable object"); throw new RuntimeException(e); } }
From source file:Main.java
public static boolean saveObject(Context context, Object data, String filename) { FileOutputStream out;// www .j a va2 s. co m ObjectOutputStream oout; try { out = context.openFileOutput(filename + ".odb", Context.MODE_PRIVATE); oout = new ObjectOutputStream(out); oout.writeObject(data); oout.flush(); out.flush(); oout.close(); out.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; }
From source file:jedi.util.serialization.Pickle.java
/** * Serializes a object into a file.//from ww w . j ava 2s .c o m * * @param o Object to be serialized. * @param f File where the serialization will occurs. * @param append defines if the write will occur in append mode. */ public static void dump(Object o, File f, boolean append) { if (o != null && f != null) { try { // File where the data will be written. FileOutputStream fos = new FileOutputStream(f, append); // Object that writes (writer) the data on the file. ObjectOutputStream oos = new ObjectOutputStream(fos); oos.flush(); oos.writeObject(o); // write the data. oos.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:Main.java
public static boolean saveObject(Context cxt, Serializable obj, String file) { FileOutputStream fos = null;//from ww w . j a va 2 s .c om ObjectOutputStream oos = null; try { fos = cxt.openFileOutput(file, Context.MODE_PRIVATE); oos = new ObjectOutputStream(fos); oos.writeObject(obj); oos.flush(); return true; } catch (Exception e) { e.printStackTrace(); return false; } finally { try { oos.close(); } catch (Exception e) { } try { fos.close(); } catch (Exception e) { } } }
From source file:de.tudarmstadt.ukp.dkpro.keyphrases.bookindexing.util.SerializationUtils.java
/** * @param object/*from w w w . j a v a 2 s . c o m*/ * object to be serialized * @param filePath * the object will be written at this location, directories will be * created according to path * @throws Exception exception */ public static void serializeToDisk(Serializable object, String filePath) throws Exception { File dir = new File(FilenameUtils.getFullPathNoEndSeparator(filePath)); if (!dir.exists()) { FileUtils.forceMkdir(dir); } else { if (dir.isFile()) { throw new IOException("Path to dir is a file!"); } } ObjectOutputStream objOut = null; objOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filePath))); objOut.writeObject(object); objOut.flush(); objOut.close(); }