Here you can find the source of serializeAndDeserialize(T instance)
@SuppressWarnings("unchecked") public static <T> T serializeAndDeserialize(T instance) throws Exception
//package com.java2s; /******************************************************************************* * Copyright (c) 2013 EclipseSource.// w w w . ja v a2 s.c om * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Ralf Sternberg - initial implementation and API ******************************************************************************/ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class Main { @SuppressWarnings("unchecked") public static <T> T serializeAndDeserialize(T instance) throws Exception { return (T) deserialize(serialize(instance)); } public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException { ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); return new ObjectInputStream(inputStream).readObject(); } public static byte[] serialize(Object object) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); new ObjectOutputStream(outputStream).writeObject(object); return outputStream.toByteArray(); } }