再谈Java序列化

package cn.vicky.chapt01;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *
 * @author Vicky.H
 */
public class Person implements java.io.Serializable {

    public Person(String fn, String ln, int a) {
        this.firstName = fn;
        this.lastName = ln;
        this.age = a;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getAge() {
        return age;
    }

    public Person getSpouse() {
        return spouse;
    }

    public void setFirstName(String value) {
        firstName = value;
    }

    public void setLastName(String value) {
        lastName = value;
    }

    public void setAge(int value) {
        age = value;
    }

    public void setSpouse(Person value) {
        spouse = value;
    }

    public static int getIndex() {
        return index;
    }

    public static void setIndex(int index) {
        Person.index = index;
    }

    public int getIndex2() {
        return index2;
    }

    public void setIndex2(int index2) {
        this.index2 = index2;
    }
    
    

    @Override
    public String toString() {
        return "[Person: firstName=" + firstName
                + " lastName=" + lastName
                + " age=" + age
                + " spouse=" + spouse.getFirstName()
                + "]";
    }
    private String firstName;
    private String lastName;
    private int age;
    private Person spouse;
    
    private static int index = 0;
    private transient int index2 = 10;

    public static void main(String args[]) {
//        try {
//            Person ted = new Person("Ted", "Neward", 39);
//            Person charl = new Person("Charlotte", "Neward", 38);
//            
//            ted.setSpouse(charl);
//            charl.setSpouse(ted);
//            
//            ted.setIndex(99);
//            ted.setIndex2(9999);
//
//            FileOutputStream fos = new FileOutputStream("tempdata.ser");
//            ObjectOutputStream oos = new ObjectOutputStream(fos);
//            oos.writeObject(ted);
//            oos.close();
//        } catch (Exception ex) {
//            System.err.println("Exception thrown during test: " + ex.toString());
//        }
        
        
        
        try {
            FileInputStream fis = new FileInputStream("tempdata.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Person ted = (Person) ois.readObject();
            ois.close();

            System.out.println(ted.getFirstName()); // "Ted"
            System.out.println(ted.getSpouse().getFirstName()); // "Charlotte"
            System.out.println("Person.getIndex() = " + Person.getIndex());
            System.out.println("ted.getIndex() = " + ted.getIndex());
            System.out.println("ted.getIndex2() = " + ted.getIndex2());
            
            // Clean up the file
            new File("tempdata.ser").delete();
        } catch (Exception ex) {
            System.err.println("Exception thrown during test: " + ex.toString());
        }

    }
}


 

 

package cn.vicky.chapt02;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *
 * @author Vicky.H
 */
enum Gender{
    MALE, FEMALE
}

public class Person implements java.io.Serializable {

    public Person(String fn, String ln, int a,Gender g) {
        this.firstName = fn;
        this.lastName = ln;
        this.age = a;
        this.gender = g;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getAge() {
        return age;
    }

    public Person getSpouse() {
        return spouse;
    }

    public void setFirstName(String value) {
        firstName = value;
    }

    public void setLastName(String value) {
        lastName = value;
    }

    public void setAge(int value) {
        age = value;
    }

    public void setSpouse(Person value) {
        spouse = value;
    }

    public static int getIndex() {
        return index;
    }

    public static void setIndex(int index) {
        Person.index = index;
    }

    public int getIndex2() {
        return index2;
    }

    public void setIndex2(int index2) {
        this.index2 = index2;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }
    
    @Override
    public String toString() {
        return "[Person: firstName=" + firstName
                + " lastName=" + lastName
                + " age=" + age
                + " spouse=" + spouse.getFirstName()
                + "]";
    }
    private String firstName;
    private String lastName;
    private int age;
    private Person spouse;
    
    private static int index = 0;
    private transient int index2 = 10;
    
    private Gender gender;

    public static void main(String args[]) {
//        try {
//            Person ted = new Person("Ted", "Neward", 39 ,Gender.FEMALE);
//            Person charl = new Person("Charlotte", "Neward", 38, Gender.MALE);
//            
//            ted.setSpouse(charl);
//            charl.setSpouse(ted);
//            
//            ted.setIndex(99);
//            ted.setIndex2(9999);
//
//            FileOutputStream fos = new FileOutputStream("tempdata.ser");
//            ObjectOutputStream oos = new ObjectOutputStream(fos);
//            oos.writeObject(ted);
//            oos.close();
//        } catch (Exception ex) {
//            System.err.println("Exception thrown during test: " + ex.toString());
//        }
        
        
        
        try {
            FileInputStream fis = new FileInputStream("tempdata.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Person ted = (Person) ois.readObject();
            ois.close();

            System.out.println(ted.getFirstName()); // "Ted"
            System.out.println(ted.getSpouse().getFirstName()); // "Charlotte"
            System.out.println("Person.getIndex() = " + Person.getIndex());
            System.out.println("ted.getIndex() = " + ted.getIndex());
            System.out.println("ted.getIndex2() = " + ted.getIndex2());
            
            System.out.println("ted.getGender() = " + ted.getGender());
            System.out.println("ted.getSpouse().getGender() = " + ted.getSpouse().getGender());
            
            // Clean up the file
            new File("tempdata.ser").delete();
        } catch (Exception ex) {
            System.err.println("Exception thrown during test: " + ex.toString());
        }

    }
}


 

 

package cn.vicky.chapt03;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *
 * @author Vicky.H
 */
enum Gender{
    MALE, FEMALE
}

public class Person implements java.io.Serializable {

    public Person() {}
    
    public Person(String fn, String ln, int a,Gender g) {
        this.firstName = fn;
        this.lastName = ln;
        this.age = a;
        this.gender = g;
    }

    private void writeObject(java.io.ObjectOutputStream stream) throws java.io.IOException    {
        // "Encrypt"/obscure the sensitive data
        age = age << 2;
        stream.defaultWriteObject();
     /*
     * 上一个技巧假设您想模糊化序列化数据,而不是对其加密或者确保它不被修改。当然,通过使用 writeObject 和 readObject 可以实现密码加密和签名管理,但其实还有更好的方式。
     * 如果需要对整个对象进行加密和签名,最简单的是将它放在一个 javax.crypto.SealedObject 和/或 java.security.SignedObject 包装器中。两者都是可序列化的,所以将对象
     * 包装在 SealedObject 中可以围绕原对象创建一种 “包装盒”。必须有对称密钥才能解密,而且密钥必须单独管理。同样,也可以将 SignedObject 用于数据验证,并且对称密钥也必须单独管理。
     * 结合使用这两种对象,便可以轻松地对序列化数据进行密封和签名,而不必强调关于数字签名验证或加密的细节。很简洁,是吧?
     */
    }

    private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, ClassNotFoundException {
        stream.defaultReadObject();
        // "Decrypt"/de-obscure the sensitive data
        age = age >> 2;
    }

    
    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getAge() {
        return age;
    }

    public Person getSpouse() {
        return spouse;
    }

    public void setFirstName(String value) {
        firstName = value;
    }

    public void setLastName(String value) {
        lastName = value;
    }

    public void setAge(int value) {
        age = value;
    }

    public void setSpouse(Person value) {
        spouse = value;
    }

    public static int getIndex() {
        return index;
    }

    public static void setIndex(int index) {
        Person.index = index;
    }

    public int getIndex2() {
        return index2;
    }

    public void setIndex2(int index2) {
        this.index2 = index2;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }
    
    @Override
    public String toString() {
        return "[Person: firstName=" + firstName
                + " lastName=" + lastName
                + " age=" + age
                + " spouse=" + spouse.getFirstName()
                + "]";
    }
    private String firstName;
    private String lastName;
    private int age;
    private Person spouse;
    
    private static int index = 0;
    private transient int index2 = 10;
    
    private Gender gender;

    public static void main(String args[]) {
        try {
            Person ted = new Person("Ted", "Neward", 39 ,Gender.FEMALE);
            Person charl = new Person("Charlotte", "Neward", 38, Gender.MALE);
            
            ted.setSpouse(charl);
            charl.setSpouse(ted);
            
            ted.setIndex(99);
            ted.setIndex2(9999);

            FileOutputStream fos = new FileOutputStream("C://tempdata.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(ted);
            oos.close();
        } catch (Exception ex) {
            System.err.println("Exception thrown during test: " + ex.toString());
            ex.printStackTrace();
        }
        
        
        
        try {
            FileInputStream fis = new FileInputStream("C://tempdata.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Person ted = (Person) ois.readObject();
            ois.close();

            System.out.println(ted.getFirstName()); // "Ted"
            System.out.println(ted.getSpouse().getFirstName()); // "Charlotte"
            System.out.println(ted.getAge());
            System.out.println(ted.getSpouse().getAge());
            
            // Clean up the file
            new File("C://tempdata.ser").delete();
        } catch (Exception ex) {
            System.err.println("Exception thrown during test: " + ex.toString());
            ex.printStackTrace();
        }

    }

}


 

package cn.vicky.chapt04;

/**
 *
 * @author Vicky.H
 */
public class Person implements java.io.Serializable {

    public Person(String fn, String ln, int a) {
        this.firstName = fn;
        this.lastName = ln;
        this.age = a;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getAge() {
        return age;
    }

    public Person getSpouse() {
        return spouse;
    }

    private Object writeReplace() throws java.io.ObjectStreamException {
        return new PersonProxy(this);
    }

    public void setFirstName(String value) {
        firstName = value;
    }

    public void setLastName(String value) {
        lastName = value;
    }

    public void setAge(int value) {
        age = value;
    }

    public void setSpouse(Person value) {
        spouse = value;
    }

    @Override
    public String toString() {
        return "[Person: firstName=" + firstName
                + " lastName=" + lastName
                + " age=" + age
                + " spouse=" + spouse.getFirstName()
                + "]";
    }
    private String firstName;
    private String lastName;
    private int age;
    private Person spouse;
}

class PersonProxy implements java.io.Serializable {

    public PersonProxy(Person orig) {
        data = orig.getFirstName() + "," + orig.getLastName() + "," + orig.getAge();
        if (orig.getSpouse() != null) {
            Person spouse = orig.getSpouse();
            data = data + "," + spouse.getFirstName() + "," + spouse.getLastName() + "," + spouse.getAge();
        }
    }
    public String data;

    private Object readResolve() throws java.io.ObjectStreamException {
        String[] pieces = data.split(",");
        Person result = new Person(pieces[0], pieces[1], Integer.parseInt(pieces[2]));
        if (pieces.length > 3) {
            result.setSpouse(new Person(pieces[3], pieces[4], Integer.parseInt(pieces[5])));
            result.getSpouse().setSpouse(result);
        }
        return result;
    }
}


 

你可能感兴趣的:(再谈Java序列化)