zugzwang
November 22nd, 2004, 05:00 PM
/*
In main()
1. myPet(Bosco) written to output stream o1.
2. myPet name changed to Lassie
3. myPet(Lassie) again written to o1.
4. myPet(Lassie) written to new output stream o2.
I would expect output to be:
out1Write1: [Bosco]
out1Write2: [Lassie]
out2Write1: [Lassie]
Instead output is:
out1Write1: [Bosco]
out1Write2: [Bosco]
out2Write1: [Lassie]
Why isn't out1Write2 = Lassie?
*/
import java.io.*;
class Animal implements Serializable {
private String name;
Animal(String n) { name = n; }
public void chgName(String s) { name = s; }
public String toString() { return ("[" + name + "]"); }
}
public class WriteObjectAnomaly {
public static void main(String[] args) throws Exception {
Animal myPet = new Animal("Bosco");
ByteArrayOutputStream buf1 = new ByteArrayOutputStream();
ObjectOutputStream o1 = new ObjectOutputStream(buf1);
o1.writeObject(myPet);
myPet.chgName("Lassie");
o1.writeObject(myPet);
ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
ObjectOutputStream o2 = new ObjectOutputStream(buf2);
o2.writeObject(myPet);
ObjectInputStream in1 = new ObjectInputStream(
new ByteArrayInputStream(buf1.toByteArray()));
ObjectInputStream in2 = new ObjectInputStream(
new ByteArrayInputStream(buf2.toByteArray()));
Animal out1Write1 = (Animal)in1.readObject();
Animal out1Write2 = (Animal)in1.readObject();
Animal out2Write1 = (Animal)in2.readObject();
System.out.println("out1Write1: " + out1Write1);
System.out.println("out1Write2: " + out1Write2);
System.out.println("out2Write1: " + out2Write1);
}
}
In main()
1. myPet(Bosco) written to output stream o1.
2. myPet name changed to Lassie
3. myPet(Lassie) again written to o1.
4. myPet(Lassie) written to new output stream o2.
I would expect output to be:
out1Write1: [Bosco]
out1Write2: [Lassie]
out2Write1: [Lassie]
Instead output is:
out1Write1: [Bosco]
out1Write2: [Bosco]
out2Write1: [Lassie]
Why isn't out1Write2 = Lassie?
*/
import java.io.*;
class Animal implements Serializable {
private String name;
Animal(String n) { name = n; }
public void chgName(String s) { name = s; }
public String toString() { return ("[" + name + "]"); }
}
public class WriteObjectAnomaly {
public static void main(String[] args) throws Exception {
Animal myPet = new Animal("Bosco");
ByteArrayOutputStream buf1 = new ByteArrayOutputStream();
ObjectOutputStream o1 = new ObjectOutputStream(buf1);
o1.writeObject(myPet);
myPet.chgName("Lassie");
o1.writeObject(myPet);
ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
ObjectOutputStream o2 = new ObjectOutputStream(buf2);
o2.writeObject(myPet);
ObjectInputStream in1 = new ObjectInputStream(
new ByteArrayInputStream(buf1.toByteArray()));
ObjectInputStream in2 = new ObjectInputStream(
new ByteArrayInputStream(buf2.toByteArray()));
Animal out1Write1 = (Animal)in1.readObject();
Animal out1Write2 = (Animal)in1.readObject();
Animal out2Write1 = (Animal)in2.readObject();
System.out.println("out1Write1: " + out1Write1);
System.out.println("out1Write2: " + out1Write2);
System.out.println("out2Write1: " + out2Write1);
}
}