2015-11-03 12:00:44 -08:00
|
|
|
// serialization/Worm.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.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Demonstrates object serialization
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
class Data implements Serializable {
|
|
|
|
private int n;
|
|
|
|
public Data(int n) { this.n = n; }
|
|
|
|
@Override
|
2016-01-25 18:05:55 -08:00
|
|
|
public String toString() {
|
|
|
|
return Integer.toString(n);
|
|
|
|
}
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public class Worm implements Serializable {
|
2016-01-25 18:05:55 -08:00
|
|
|
private static SplittableRandom rand = new SplittableRandom(47);
|
2015-06-15 17:47:35 -07:00
|
|
|
private Data[] d = {
|
|
|
|
new Data(rand.nextInt(10)),
|
|
|
|
new Data(rand.nextInt(10)),
|
|
|
|
new Data(rand.nextInt(10))
|
|
|
|
};
|
|
|
|
private Worm next;
|
|
|
|
private char c;
|
|
|
|
// Value of i == number of segments
|
|
|
|
public Worm(int i, char x) {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Worm constructor: " + i);
|
2015-06-15 17:47:35 -07:00
|
|
|
c = x;
|
|
|
|
if(--i > 0)
|
|
|
|
next = new Worm(i, (char)(x + 1));
|
|
|
|
}
|
|
|
|
public Worm() {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("No-arg constructor");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
StringBuilder result = new StringBuilder(":");
|
|
|
|
result.append(c);
|
|
|
|
result.append("(");
|
|
|
|
for(Data dat : d)
|
|
|
|
result.append(dat);
|
|
|
|
result.append(")");
|
|
|
|
if(next != null)
|
|
|
|
result.append(next);
|
|
|
|
return result.toString();
|
|
|
|
}
|
2016-01-25 18:05:55 -08:00
|
|
|
public static void
|
|
|
|
main(String[] args) throws ClassNotFoundException,
|
|
|
|
IOException {
|
2015-06-15 17:47:35 -07:00
|
|
|
Worm w = new Worm(6, 'a');
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("w = " + w);
|
2016-09-06 12:32:51 -06:00
|
|
|
try(
|
|
|
|
ObjectOutputStream out = new ObjectOutputStream(
|
|
|
|
new FileOutputStream("worm.dat"))
|
|
|
|
) {
|
2015-06-15 17:47:35 -07:00
|
|
|
out.writeObject("Worm storage\n");
|
|
|
|
out.writeObject(w);
|
|
|
|
}
|
2016-09-06 12:32:51 -06:00
|
|
|
try(
|
|
|
|
ObjectInputStream in = new ObjectInputStream(
|
|
|
|
new FileInputStream("worm.dat"))
|
|
|
|
) {
|
2015-12-15 11:47:04 -08:00
|
|
|
String s = (String)in.readObject();
|
|
|
|
Worm w2 = (Worm)in.readObject();
|
|
|
|
System.out.println(s + "w2 = " + w2);
|
|
|
|
}
|
2016-09-06 12:32:51 -06:00
|
|
|
try(
|
|
|
|
ByteArrayOutputStream bout =
|
|
|
|
new ByteArrayOutputStream();
|
|
|
|
ObjectOutputStream out2 =
|
|
|
|
new ObjectOutputStream(bout)
|
|
|
|
) {
|
2015-12-15 11:47:04 -08:00
|
|
|
out2.writeObject("Worm storage\n");
|
|
|
|
out2.writeObject(w);
|
|
|
|
out2.flush();
|
2016-09-06 12:32:51 -06:00
|
|
|
try(
|
|
|
|
ObjectInputStream in2 = new ObjectInputStream(
|
|
|
|
new ByteArrayInputStream(
|
|
|
|
bout.toByteArray()))
|
|
|
|
) {
|
2015-12-15 11:47:04 -08:00
|
|
|
String s = (String)in2.readObject();
|
|
|
|
Worm w3 = (Worm)in2.readObject();
|
|
|
|
System.out.println(s + "w3 = " + w3);
|
|
|
|
}
|
|
|
|
}
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
Worm constructor: 6
|
|
|
|
Worm constructor: 5
|
|
|
|
Worm constructor: 4
|
|
|
|
Worm constructor: 3
|
|
|
|
Worm constructor: 2
|
|
|
|
Worm constructor: 1
|
2016-07-22 14:45:35 -06:00
|
|
|
w = :a(571):b(079):c(688):d(307):e(052):f(402)
|
2015-06-15 17:47:35 -07:00
|
|
|
Worm storage
|
2016-07-22 14:45:35 -06:00
|
|
|
w2 = :a(571):b(079):c(688):d(307):e(052):f(402)
|
2015-06-15 17:47:35 -07:00
|
|
|
Worm storage
|
2016-07-22 14:45:35 -06:00
|
|
|
w3 = :a(571):b(079):c(688):d(307):e(052):f(402)
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|