I have an Issue deserialising a Java Instance with 3 fields.
2 of the fields are being deserialised as I would expect.
The 3rd is being initialised as defined in the Class.
I would expect it to be deserialised too.
I am using openJDK 17.
Below is some example code.
- Run the Programme as is
- WAIT a few seconds
- comment in & out as indicated (3 TODO's) & run again
First time through I get this:
11:09:02 SerialTest then.: 11:09:02.870987 msThen.: 1680685742677 msNow.: 1680685742988
11:09:04 SerialTest then.: 11:09:02.870987 msThen.: 1680685742677 msNow.: 1680685744990
11:09:06 SerialTest then.: 11:09:02.870987 msThen.: 1680685742677 msNow.: 1680685746992
11:09:08 SerialTest then.: 11:09:02.870987 msThen.: 1680685742677 msNow.: 1680685748993
11:09:10 SerialTest then.: 11:09:02.870987 msThen.: 1680685742677 msNow.: 1680685750994
Second time, this:
11:09:40 Why this?! then.: 11:09:02.870987 msThen.: 1680685742677 msNow.: 1680685780744
11:09:42 Why this?! then.: 11:09:02.870987 msThen.: 1680685742677 msNow.: 1680685782747
11:09:44 Why this?! then.: 11:09:02.870987 msThen.: 1680685742677 msNow.: 1680685784749
11:09:46 Why this?! then.: 11:09:02.870987 msThen.: 1680685742677 msNow.: 1680685786751
11:09:48 Why this?! then.: 11:09:02.870987 msThen.: 1680685742677 msNow.: 1680685788751
The "then" Millis & LocalTime are being deserialised as I had expected & remain constant.
Can anyone help me to understand why className is not being deserialised?
import java.io.*;
import java.nio.file.*;
import java.time.*;
import java.time.temporal.*;
public final class SerialTest implements Serializable {
private static final long serialVersionUID = -5422947008060509280L;
private static final Path PATH = Path.of("SerialTest.ser");
public final long millisThen = System.currentTimeMillis();
public final String dateTimeThen = LocalTime.now().toString();
public final String className = SerialTest.class.getName(); // TODO Comment out
// public final String className = "Why this?!"; // TODO Comment in
public static void main(final String[] args) throws Exception {
Files.write(PATH, new SerialTest().serialize()); // TODO Comment out
deserialize(); sleep(Duration.ofSeconds(2));
deserialize(); sleep(Duration.ofSeconds(2));
deserialize(); sleep(Duration.ofSeconds(2));
deserialize(); sleep(Duration.ofSeconds(2));
deserialize(); sleep(Duration.ofSeconds(2));
}
private static void sleep(final Duration duration) {
try {Thread.sleep(duration.toMillis());} catch (final Exception e) {}
}
private static void deserialize() throws Exception {
final InputStream ist = new ByteArrayInputStream(Files.readAllBytes(PATH));
final SerialTest ser = (SerialTest) new ObjectInputStream(ist).readObject();
System.out.print ("" + LocalTime.now().truncatedTo(ChronoUnit.SECONDS));
System.out.print (" " + ser.className);
System.out.print (" then.: " + ser.dateTimeThen);
System.out.print (" msThen.: " + ser.millisThen);
System.out.println(" msNow.: " + System.currentTimeMillis());
}
public byte[] serialize() throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
try {
oos.writeObject(this);
}
finally {
oos.close();
}
return baos.toByteArray();
}
}