2015-11-03 12:00:44 -08:00
|
|
|
// serialization/MyWorld.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
class House implements Serializable {}
|
|
|
|
|
|
|
|
class Animal implements Serializable {
|
|
|
|
private String name;
|
|
|
|
private House preferredHouse;
|
|
|
|
Animal(String nm, House h) {
|
|
|
|
name = nm;
|
|
|
|
preferredHouse = h;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return name + "[" + super.toString() +
|
|
|
|
"], " + preferredHouse + "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class MyWorld {
|
2017-01-22 16:48:11 -08:00
|
|
|
public static void main(String[] args) {
|
2015-06-15 17:47:35 -07:00
|
|
|
House house = new House();
|
|
|
|
List<Animal> animals = new ArrayList<>();
|
2017-01-22 16:48:11 -08:00
|
|
|
animals.add(
|
|
|
|
new Animal("Bosco the dog", house));
|
|
|
|
animals.add(
|
|
|
|
new Animal("Ralph the hamster", house));
|
|
|
|
animals.add(
|
|
|
|
new Animal("Molly the cat", house));
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("animals: " + animals);
|
2016-09-06 12:32:51 -06:00
|
|
|
try(
|
|
|
|
ByteArrayOutputStream buf1 =
|
|
|
|
new ByteArrayOutputStream();
|
|
|
|
ObjectOutputStream o1 =
|
|
|
|
new ObjectOutputStream(buf1)
|
|
|
|
) {
|
2016-01-25 18:05:55 -08:00
|
|
|
o1.writeObject(animals);
|
|
|
|
o1.writeObject(animals); // Write a 2nd set
|
|
|
|
// Write to a different stream:
|
2016-09-06 12:32:51 -06:00
|
|
|
try(
|
|
|
|
ByteArrayOutputStream buf2 =
|
|
|
|
new ByteArrayOutputStream();
|
|
|
|
ObjectOutputStream o2 =
|
|
|
|
new ObjectOutputStream(buf2)
|
|
|
|
) {
|
2016-01-25 18:05:55 -08:00
|
|
|
o2.writeObject(animals);
|
|
|
|
// Now get them back:
|
2016-09-06 12:32:51 -06:00
|
|
|
try(
|
|
|
|
ObjectInputStream in1 =
|
|
|
|
new ObjectInputStream(
|
|
|
|
new ByteArrayInputStream(
|
|
|
|
buf1.toByteArray()));
|
|
|
|
ObjectInputStream in2 =
|
|
|
|
new ObjectInputStream(
|
|
|
|
new ByteArrayInputStream(
|
|
|
|
buf2.toByteArray()))
|
|
|
|
) {
|
2016-01-25 18:05:55 -08:00
|
|
|
List
|
|
|
|
animals1 = (List)in1.readObject(),
|
|
|
|
animals2 = (List)in1.readObject(),
|
|
|
|
animals3 = (List)in2.readObject();
|
2017-01-22 16:48:11 -08:00
|
|
|
System.out.println(
|
|
|
|
"animals1: " + animals1);
|
|
|
|
System.out.println(
|
|
|
|
"animals2: " + animals2);
|
|
|
|
System.out.println(
|
|
|
|
"animals3: " + animals3);
|
2016-01-25 18:05:55 -08:00
|
|
|
}
|
|
|
|
}
|
2017-01-22 16:48:11 -08:00
|
|
|
} catch(IOException | ClassNotFoundException e) {
|
|
|
|
throw new RuntimeException(e);
|
2016-01-25 18:05:55 -08:00
|
|
|
}
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2017-05-10 11:45:39 -06:00
|
|
|
animals: [Bosco the dog[Animal@15db9742],
|
|
|
|
House@6d06d69c
|
2017-05-03 12:00:00 -06:00
|
|
|
, Ralph the hamster[Animal@7852e922], House@6d06d69c
|
|
|
|
, Molly the cat[Animal@4e25154f], House@6d06d69c
|
2015-06-15 17:47:35 -07:00
|
|
|
]
|
2017-05-10 11:45:39 -06:00
|
|
|
animals1: [Bosco the dog[Animal@7ba4f24f],
|
|
|
|
House@3b9a45b3
|
2017-05-03 12:00:00 -06:00
|
|
|
, Ralph the hamster[Animal@7699a589], House@3b9a45b3
|
|
|
|
, Molly the cat[Animal@58372a00], House@3b9a45b3
|
2015-06-15 17:47:35 -07:00
|
|
|
]
|
2017-05-10 11:45:39 -06:00
|
|
|
animals2: [Bosco the dog[Animal@7ba4f24f],
|
|
|
|
House@3b9a45b3
|
2017-05-03 12:00:00 -06:00
|
|
|
, Ralph the hamster[Animal@7699a589], House@3b9a45b3
|
|
|
|
, Molly the cat[Animal@58372a00], House@3b9a45b3
|
2015-06-15 17:47:35 -07:00
|
|
|
]
|
2017-05-10 11:45:39 -06:00
|
|
|
animals3: [Bosco the dog[Animal@4dd8dc3],
|
|
|
|
House@6d03e736
|
2017-05-03 12:00:00 -06:00
|
|
|
, Ralph the hamster[Animal@568db2f2], House@6d03e736
|
|
|
|
, Molly the cat[Animal@378bf509], House@6d03e736
|
2015-06-15 17:47:35 -07:00
|
|
|
]
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|