2015-11-03 12:00:44 -08:00
|
|
|
// serialization/Blip3.java
|
2020-10-07 13:35:40 -06:00
|
|
|
// (c)2020 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
|
|
|
// Reconstructing an externalizable object
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
public class Blip3 implements Externalizable {
|
|
|
|
private int i;
|
|
|
|
private String s; // No initialization
|
|
|
|
public Blip3() {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Blip3 Constructor");
|
2015-06-15 17:47:35 -07:00
|
|
|
// s, i not initialized
|
|
|
|
}
|
|
|
|
public Blip3(String x, int a) {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Blip3(String x, int a)");
|
2015-06-15 17:47:35 -07:00
|
|
|
s = x;
|
|
|
|
i = a;
|
2015-11-03 12:00:44 -08:00
|
|
|
// s & i initialized only in non-no-arg constructor.
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String toString() { return s + i; }
|
|
|
|
@Override
|
|
|
|
public void writeExternal(ObjectOutput out)
|
|
|
|
throws IOException {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Blip3.writeExternal");
|
2015-06-15 17:47:35 -07:00
|
|
|
// You must do this:
|
|
|
|
out.writeObject(s);
|
|
|
|
out.writeInt(i);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void readExternal(ObjectInput in)
|
|
|
|
throws IOException, ClassNotFoundException {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Blip3.readExternal");
|
2015-06-15 17:47:35 -07:00
|
|
|
// You must do this:
|
|
|
|
s = (String)in.readObject();
|
|
|
|
i = in.readInt();
|
|
|
|
}
|
2017-01-22 16:48:11 -08:00
|
|
|
public static void main(String[] args) {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Constructing objects:");
|
2015-06-15 17:47:35 -07:00
|
|
|
Blip3 b3 = new Blip3("A String ", 47);
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(b3);
|
2016-09-06 12:32:51 -06:00
|
|
|
try(
|
|
|
|
ObjectOutputStream o = new ObjectOutputStream(
|
|
|
|
new FileOutputStream("Blip3.serialized"))
|
|
|
|
) {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Saving object:");
|
2015-06-15 17:47:35 -07:00
|
|
|
o.writeObject(b3);
|
2017-01-22 16:48:11 -08:00
|
|
|
} catch(IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
// Now get it back:
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Recovering b3:");
|
2016-09-06 12:32:51 -06:00
|
|
|
try(
|
|
|
|
ObjectInputStream in = new ObjectInputStream(
|
|
|
|
new FileInputStream("Blip3.serialized"))
|
|
|
|
) {
|
2015-12-15 11:47:04 -08:00
|
|
|
b3 = (Blip3)in.readObject();
|
2017-01-22 16:48:11 -08:00
|
|
|
} catch(IOException | ClassNotFoundException e) {
|
|
|
|
throw new RuntimeException(e);
|
2015-12-15 11:47:04 -08:00
|
|
|
}
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(b3);
|
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
|
|
|
Constructing objects:
|
|
|
|
Blip3(String x, int a)
|
|
|
|
A String 47
|
|
|
|
Saving object:
|
|
|
|
Blip3.writeExternal
|
|
|
|
Recovering b3:
|
|
|
|
Blip3 Constructor
|
|
|
|
Blip3.readExternal
|
|
|
|
A String 47
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|