Index Chap11 stan (3) 453 10 abc.com.pl 6 006 17 (2) 08 (17) Bogdanowicz Marta O dysleksji czyli specyficznych trudnoÂśc Stefan Żeromski PrzedwioÂśnie Kratochvil Stanisław Psychoterapia Kierunki metody badania A Brodziak Jestes Niesmiertelny |
[ Pobierz całość w formacie PDF ] .[ Add Comment ]VersioningIt’s possible that you might wantto change the version of a serializable class (objects of the original classmight be stored in a database, for example).This is supported but you’llprobably do it only in special cases, and it requires an extra depth ofunderstanding that we will not attempt to achieve here.The JDK HTML documentsdownloadable from java.sun.com cover this topic quite thoroughly.[ Add Comment ]You will also notice in the JDK HTMLdocumentation many comments that begin with:Warning: Serializedobjects of this class will not be compatible with future Swing releases.Thecurrent serialization support is appropriate for short term storage or RMIbetween applications.This is because the versioning mechanismis too simple to work reliably in all situations, especially with JavaBeans.They’re working on a correction for the design, and that’s what thewarning is about.[ Add Comment ]Using persistenceIt’s quite appealing to useserialization technology to store some of the state ofyour program so that you can easily restore the program to the current statelater.But before you can do this, some questions must be answered.What happensif you serialize two objects that both have a reference to a third object? Whenyou restore those two objects from their serialized state, do you get only oneoccurrence of the third object? What if you serialize your two objects toseparate files and deserialize them in different parts of your code?[ Add Comment ]Here’s an example that shows theproblem://: c11:MyWorld.javaimport java.io.*;import java.util.*;class House implements Serializable {}class Animal implements Serializable {String name;House preferredHouse;Animal(String nm, House h) {name = nm;preferredHouse = h;}public String toString() {return name + "[" + super.toString() +"], " + preferredHouse + "\n";}}public class MyWorld {public static void main(String[] args)throws IOException, ClassNotFoundException {House house = new House();ArrayList animals = new ArrayList();animals.add(new Animal("Bosco the dog", house));animals.add(new Animal("Ralph the hamster", house));animals.add(new Animal("Fronk the cat", house));System.out.println("animals: " + animals);ByteArrayOutputStream buf1 =new ByteArrayOutputStream();ObjectOutputStream o1 =new ObjectOutputStream(buf1);o1.writeObject(animals);o1.writeObject(animals); // Write a 2nd set// Write to a different stream:ByteArrayOutputStream buf2 =new ByteArrayOutputStream();ObjectOutputStream o2 =new ObjectOutputStream(buf2);o2.writeObject(animals);// Now get them back:ObjectInputStream in1 =new ObjectInputStream(new ByteArrayInputStream(buf1.toByteArray()));ObjectInputStream in2 =new ObjectInputStream(new ByteArrayInputStream(buf2.toByteArray()));ArrayList animals1 =(ArrayList)in1.readObject();ArrayList animals2 =(ArrayList)in1.readObject();ArrayList animals3 =(ArrayList)in2.readObject();System.out.println("animals1: " + animals1);System.out.println("animals2: " + animals2);System.out.println("animals3: " + animals3);}} ///:~One thing that’s interesting hereis that it’s possible to use object serialization to and from a byte arrayas a way of doing a “deep copy” of any object that’sSerializable.(A deep copy means that you’re duplicating the entireweb of objects, rather than just the basic object and its references.) Copyingis covered in depth in Appendix A.[ Add Comment ]Animal objects contain fields oftype House.In main( ), an ArrayList of theseAnimals is created and it is serialized twice to one stream and thenagain to a separate stream.When these are deserialized and printed, you see thefollowing results for one run (the objects will be in different memory locationseach run):animals: [Bosco the dog[Animal@1cc76c], House@1cc769, Ralph the hamster[Animal@1cc76d], House@1cc769, Fronk the cat[Animal@1cc76e], House@1cc769]animals1: [Bosco the dog[Animal@1cca0c], House@1cca16, Ralph the hamster[Animal@1cca17], House@1cca16, Fronk the cat[Animal@1cca1b], House@1cca16]animals2: [Bosco the dog[Animal@1cca0c], House@1cca16, Ralph the hamster[Animal@1cca17], House@1cca16, Fronk the cat[Animal@1cca1b], House@1cca16]animals3: [Bosco the dog[Animal@1cca52], House@1cca5c, Ralph the hamster[Animal@1cca5d], House@1cca5c, Fronk the cat[Animal@1cca61], House@1cca5c]Of course you expect that thedeserialized objects have different addresses from their originals.But noticethat in animals1 and animals2 the same addresses appear, includingthe references to the House object that both share.On the other hand,when animals3 is recovered the system has no way of knowing that theobjects in this other stream are aliases of the objects in the first stream, soit makes a completely different web of objects.[ Add Comment ]As long as you’re serializingeverything to a single stream, you’ll be able to recover the same web ofobjects that you wrote, with no accidental duplication of objects [ Pobierz całość w formacie PDF ] |
||||
Wszelkie Prawa Zastrzeżone! Kawa była słaba i bez smaku. Nie miała treści, a jedynie formę. Design by SZABLONY.maniak.pl. | |||||