package ch18io;

import java.io.*;
import java.util.*;
import static commons.util.Print.*;

/**
 * <pre>
 *  Output: (Sample)
 * animals: [Bosco the dog[Animal@addbf1], House@42e816
 * , Ralph the hamster[Animal@9304b1], House@42e816
 * , Molly the cat[Animal@190d11], House@42e816
 * ]
 * animals1: [Bosco the dog[Animal@de6f34], House@156ee8e
 * , Ralph the hamster[Animal@47b480], House@156ee8e
 * , Molly the cat[Animal@19b49e6], House@156ee8e
 * ]
 * animals2: [Bosco the dog[Animal@de6f34], House@156ee8e
 * , Ralph the hamster[Animal@47b480], House@156ee8e
 * , Molly the cat[Animal@19b49e6], House@156ee8e
 * ]
 * animals3: [Bosco the dog[Animal@10d448], House@e0e1c6
 * , Ralph the hamster[Animal@6ca1c], House@e0e1c6
 * , Molly the cat[Animal@1bf216a], House@e0e1c6
 * ]
 * </pre>
 */
class House implements Serializable {
}

class Animal implements Serializable {
	private String name;
	private House preferredHouse;

	Animal(String nm, House h) {
		name = nm;
		preferredHouse = h;
	}

	public String toString() {
		return name + "[" + super.toString() + "], " + preferredHouse + "\n";
	}
}

public class D40_MyWorld {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		House house = new House();
		List<Animal> animals = new ArrayList<Animal>();
		animals.add(new Animal("Bosco the dog", house));
		animals.add(new Animal("Ralph the hamster", house));
		animals.add(new Animal("Molly the cat", house));
		print("animals: " + animals);
		ByteArrayOutputStream buf1 = new ByteArrayOutputStream();
		ObjectOutputStream o1 = new ObjectOutputStream(buf1);
		o1.writeObject(animals);
		o1.writeObject(animals); // Write a 2nd set
		// Write to a different stream:
		ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
		ObjectOutputStream o2 = new ObjectOutputStream(buf2);
		o2.writeObject(animals);
		// Now get them back:
		ObjectInputStream in1 = new ObjectInputStream(new ByteArrayInputStream(buf1.toByteArray()));
		ObjectInputStream in2 = new ObjectInputStream(new ByteArrayInputStream(buf2.toByteArray()));
		List animals1 = (List) in1.readObject(), animals2 = (List) in1.readObject(), animals3 = (List) in2.readObject();
		print("animals1: " + animals1);
		print("animals2: " + animals2);
		print("animals3: " + animals3);
	}
}