Serializare

Salvati urmatoarele secvente de cod in fisiere separate, grupate in directoare separate,
compilati si executati programele, urmand procedura descrisa in primul laborator.


1. Salvarea si restaurarea unui graf de obiecte

File: GraphObject.java
import java.io.*;
import java.net.*;

public class GraphObject  implements Serializable {

	public URL o1;
	public URL o2;

	public GraphObject(URL o1, URL o2) {
		this.o1 = o1;
		this.o2 = o2;
	}
	public String toString() {
		return new String("o1 = " + o1 + " o2 = " + o2);
	}
}

File: SaveObject.java
import java.io.*;
import java.net.*;

public class SaveObject {
	static GraphObject go;
	public static void main (String args[]) {
		try {
			FileOutputStream fout = new FileOutputStream("test");
			ObjectOutputStream sout = new ObjectOutputStream (fout);
			URL o1 = new URL("http://www.upb.ro");
			URL o2 = o1;
			URL o3 = o1;
			go = new GraphObject(o1,o2);
			sout.writeObject(go);
			sout.writeObject(o3);
			sout.flush();
			System.out.println("Object wrote: " + go);
			System.out.println("Object wrote: " + o3);
			System.out.println("(go.o1 == go.o2)&&(go.o1 == o3) is: " + ((go.o1 == go.o2)&&(go.o1 == o3)));
			sout.close();
			fout.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

File: RestoreObject.java
import java.io.*;
import java.net.*;

public class RestoreObject {

	static GraphObject go;

	public static void main (String args[]) {
		try {
			FileInputStream fin = new FileInputStream("test");
			ObjectInputStream sin = new ObjectInputStream(fin);
			go = (GraphObject)sin.readObject();
			System.out.println("Object read: " + go);
			URL o3 = (URL)sin.readObject();
			System.out.println("Object read: " + o3);
			System.out.println("(go.o1 == go.o2)&&(go.o1 == o3) is: " + ((go.o1 == go.o2)&&(go.o1 == o3)));

			sin.close();
			fin.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


2. Clonarea unui obiect fara duplicarea obiectelor referite

File: SomeObject.java
import java.io.Serializable;

public class SomeObject implements Serializable {
	private static final long serialVersionUID = 1L;
	String name;
	SomeObject(String name){
		this.name = name;
	}
	String getName(){
		return name;
	}
	void modifyName(String name) {
		this.name = name;
	}
}
File: Something.java
public class Something implements Cloneable {
	SomeObject o;
	Something(String s){
		o = new SomeObject(s);
	}
	void modify(String s){
		o.modifyName(s);
	}
	void print(){
		System.out.println(o.getName());
	}
	public Object clone() {
		try {
			return super.clone();
		}catch(CloneNotSupportedException e){
			e.printStackTrace();
			return null;
		}
	}
}
File: SomethingTest.java
/* testare Something: clonare fara duplicarea obiectelor referite */
public class SomethingTest {
	public static void main(String[] argv){
		Something s1 = new Something("aaaa");
		Something s2 = (Something)s1.clone();
		System.out.println("Info about s1: ");
		s1.print();
		System.out.println("Info about s2: ");
		s2.print();
		s1.modify("cccccccc");
		System.out.println("Info about s1: ");
		s1.print();
		System.out.println("Info about s2: ");
		s2.print();
	}
}

3. Clonarea unui obiect cu duplicarea obiectelor referite

File: Cloning.java
import java.io.*;

public class Cloning implements Cloneable {
	public Object clone() {
		try {
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			ObjectOutputStream out = new ObjectOutputStream(bout);
			out.writeObject(this);
			out.close();
			ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
			ObjectInputStream in = new ObjectInputStream(bin);
			Object ret = in.readObject();
			in.close();
			return ret;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}
File: SomethingCC.java
import java.io.*;

public class SomethingCC extends Cloning implements Serializable {
	private static final long serialVersionUID = 2L;
	SomeObject o;
	SomethingCC(String s){
		o = new SomeObject(s);
	}
	void modify(String s){
		o.modifyName(s);
	}
	void print(){
		System.out.println(o.getName());
	}
}
File: SomethingCCTest.java
/* testare SomethingCC: clonare cu duplicarea obiectelor referite */
public class SomethingCCTest {
	public static void main(String[] argv){
		SomethingCC scc1 = new SomethingCC("aaaa");
		SomethingCC scc2 = (SomethingCC)scc1.clone();
		System.out.println("Info about scc1: ");
		scc1.print();
		System.out.println("Info about scc2: ");
		scc2.print();
		scc1.modify("cccccccc");
		System.out.println("Info about scc1: ");
		scc1.print();
		System.out.println("Info about scc2: ");
		scc2.print();
	}
}