import java.io.ObjectOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ByteArrayInputStream;
import java.lang.Exception;

public class Serializing {

	public static void main(String [] args) {
		try {
			// Create an ObjectOutputStream
			ByteArrayOutputStream ba_out = new
				ByteArrayOutputStream();
			ObjectOutputStream obj_out = new
				ObjectOutputStream (ba_out);

			// Create the object to searilize
			Data myObject = new Data("foo", "bar");

			// Write object to Stream
			obj_out.writeObject ( myObject );
			byte[] outBytes = ba_out.toByteArray();
			System.out.println("["+ ba_out + "]");

			// ------------------------------
			// On the receiving side
			// Create an ObjectInputStream
			byte[] inBytes = outBytes;
			ByteArrayInputStream ba_in = new 
				ByteArrayInputStream(inBytes);
			// Read object using ObjectInputStream
			ObjectInputStream obj_in = 
				new ObjectInputStream (ba_in);
			Object obj = obj_in.readObject();

			// Do stuff with the object
			if (obj instanceof Data)
			{
				Data d = (Data) obj;
				System.out.println("User "+ d.username);
			}


		} catch(Exception ex) { ex.printStackTrace(); }
	}
}

