Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.tectonica.util;
 
public class SerializeUtil
	public static byte[] objToBytes(Object obj)
	{
		if (obj == null)
			return null;
		{
			try (ObjectOutput out = new ObjectOutputStream(bos))
			{
				out.writeObject(obj);
				return bos.toByteArray();
			}
			catch (IOException e)
			{
				throw new RuntimeException(e);
			}
		}
		catch (IOException e)
		{
			throw new RuntimeException(e);
		}
	}
	public static Object bytesToObj(byte[] bytes)
	{
		if (bytes == null)
			return null;
		try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);)
		{
			try (ObjectInput in = new ObjectInputStream(bis);)
			{
				return in.readObject();
			}
			{
				throw new RuntimeException(e);
			}
		}
		catch (IOException e)
		{
			throw new RuntimeException(e);
		}
	}
	@SuppressWarnings("unchecked")
	public static <V> V bytesToObj(byte[] bytesClass<V> clz)
	{
		return (V) bytesToObj(bytes);
	}

an inefficient way of cloning a Serializable object. use only temporarily. better solutions include frameworks such as Kryo.
	@SuppressWarnings("unchecked")
	public static <V> V copyOf(V obj)
	{
		if (obj == null)
			return null;
		return (V) bytesToObj(objToBytes(obj));
	}
New to GrepCode? Check out our FAQ X