List of usage examples for java.io ObjectOutputStream writeObject
public final void writeObject(Object obj) throws IOException
From source file:com.bitranger.parknshop.util.ObjUtils.java
public static byte[] toBytes(Object obj) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream objOut = null; byte[] plainObj = null; try {// w w w . j av a2 s. c o m objOut = new ObjectOutputStream(baos); objOut.writeObject(obj); plainObj = baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e.getMessage() + "\n\tCAUSE: " + e.getCause(), e); } finally { try { baos.close(); if (objOut != null) { objOut.close(); } } catch (IOException e) { throw new RuntimeException(e.getMessage() + "\n\tCAUSE: " + e.getCause(), e); } } return plainObj; }
From source file:io.github.tavernaextras.biocatalogue.model.Util.java
/** * This method is "clones" an object supplied as an argument. It uses * serialisation to achieve this (as opposed to manually implementing deep * copying of all referenced objects in the graph of the provided object). * This technique is used to make sure that the new object will be exact * replica, but totally independent of the original one. * //w ww . j av a 2s. c o m * Note that this code works ~100 times slower than it would do if deep copying * was implemented. However, this will not be used in tight loops (and in loops * at all), so for one-off tasks it is fine. * * @author Dave Miller<br/> * Original version of the code in this method is taken from * <a href="http://www.javaworld.com/javaworld/javatips/jw-javatip76.html?page=2"> * http://www.javaworld.com/javaworld/javatips/jw-javatip76.html?page=2 * </a> [accessed on 25/Feb/2010]. * <br/><br/> * * @author Subhajit Dasgupta<br/> * Example of using an alternative class loader during object de-serialisation * was taken from * <a href="http://blogs.sun.com/adventures/entry/desrializing_objects_custom_class_loaders"> * http://blogs.sun.com/adventures/entry/desrializing_objects_custom_class_loaders * </a> [accessed on 29/Mar/2010]. * * @return Deep copy of the provided object. If deep copying doesn't succeed, * <code>null</code> is returned. */ public static Object deepCopy(Object objectToCopy) { // a "safety net" - a class loader of BioCatalogue perspective may be used in // de-serialisation process to make sure that all classes are recognised // (system class loader may not be able to "see" all BioCatalogue plugin's files, // but just those in Taverna's /lib folder) final ClassLoader[] customClassLoaders = new ClassLoader[] { BioCataloguePerspective.class.getClassLoader() }; try { ObjectOutputStream oos = null; ObjectInputStream ois = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); // serialise and pass the object oos.writeObject(objectToCopy); oos.flush(); // read and return the new object ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); ois = new ObjectInputStream(bin) { /** * <code>resolveClass()</code> method is overridden to make use of * custom ClassLoader in the de-serialisation process. * <br/> * This is needed to make sure that the ClassLoader of the BioCatalogue * perspective is used as opposed to the system ClassLoader which will * only be able to see classes from Taverna's /lib folder. */ protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { String className = desc.getName(); try { // attempt to use default class loader return Class.forName(className); } catch (ClassNotFoundException exc) { // default class loader was unable to locate a required class - // attempt to use one of the provided class loaders for (ClassLoader cl : customClassLoaders) { try { return cl.loadClass(className); } catch (ClassNotFoundException e) { /* do nothing here - there may be other class loaders to try */ } } // none of the class loaders was able to recognise the currently // de-serialised class, so it's indeed an exception throw new ClassNotFoundException(className + " -- neither system, nor alternative class loaders were able to load this class"); } } }; return ois.readObject(); } catch (Exception e) { logger.error("Could not perform deep copy of " + objectToCopy.getClass() + " instance", e); } finally { oos.close(); ois.close(); } } catch (Exception e) { logger.error( "Could not close object streams during deep copy of " + objectToCopy.getClass() + " instance"); } // Error occurred - couldn't produce the deep copy... return null; }
From source file:MainClass.java
private void writeObject(ObjectOutputStream stream) throws IOException { stream.defaultWriteObject(); stream.writeObject(b); }
From source file:javaslang.jackson.datatype.serialize.SerializableSerializer.java
@Override Object toJavaObj(T value) throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(); ObjectOutputStream stream = new ObjectOutputStream(buf); stream.writeObject(value); return buf.toByteArray(); }
From source file:grails.plugin.cache.web.filter.redis.GrailsSerializer.java
public void serialize(Object object, OutputStream outputStream) throws IOException { if (!(object instanceof Serializable)) { throw new IllegalArgumentException( getClass().getSimpleName() + " requires a Serializable payload but received an object of type [" + object.getClass().getName() + "]"); }//from ww w . j a va 2 s .co m ObjectOutputStream oos = new ObjectOutputStream(outputStream); oos.writeObject(object); oos.flush(); }
From source file:com.evozi.droidsniff.objects.CookieWrapper.java
private void writeObject(ObjectOutputStream out) throws IOException { out.writeObject(cookie.getDomain()); out.writeObject(cookie.getName());// www .j av a2s.c o m out.writeObject(cookie.getPath()); out.writeObject(cookie.getValue()); }
From source file:de.dfki.asr.compass.model.AbstractCompassEntity.java
public AbstractCompassEntity deepCopy() throws IOException, ClassNotFoundException { AbstractCompassEntity entity = null; forceEagerFetch();/* w w w . j a v a 2s.c om*/ // Write the object out to a byte array ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(this); out.flush(); out.close(); // Make an input stream from the byte array and read // a copy of the object back in. ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray())); entity = (AbstractCompassEntity) in.readObject(); entity.clearIdsAfterDeepCopy(); return entity; }
From source file:com.blazeroni.reddit.http.SerializableCookie.java
private void writeObject(ObjectOutputStream out) throws IOException { out.writeObject(this.cookie.getName()); out.writeObject(this.cookie.getValue()); out.writeObject(this.cookie.getComment()); out.writeObject(this.cookie.getDomain()); out.writeObject(this.cookie.getExpiryDate()); out.writeObject(this.cookie.getPath()); out.writeInt(this.cookie.getVersion()); out.writeBoolean(this.cookie.isSecure()); }
From source file:cz.seznam.euphoria.hadoop.SerializableWritableTest.java
@SuppressWarnings("unchecked") @Test/*from w w w . j ava 2 s . c om*/ public void testSerialization() throws Exception { Configuration conf = new Configuration(); conf.set("key", "value"); SerializableWritable<Configuration> sw = new SerializableWritable<>(conf); // clone by serialization ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(sw); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); sw = (SerializableWritable<Configuration>) ois.readObject(); Configuration newConf = sw.getWritable(); assertEquals(conf.get("key"), newConf.get("key")); }
From source file:cn.caimatou.canting.utils.http.asynchttp.SerializableCookie.java
private void writeObject(ObjectOutputStream out) throws IOException { out.writeObject(cookie.getName()); out.writeObject(cookie.getValue());//from w w w .j av a 2 s . c o m out.writeObject(cookie.getComment()); out.writeObject(cookie.getDomain()); out.writeObject(cookie.getExpiryDate()); out.writeObject(cookie.getPath()); out.writeInt(cookie.getVersion()); out.writeBoolean(cookie.isSecure()); }