Java 利用 Externalizable 实现序列化

原地址:http://201211131343.iteye.com/blog/1772780

Java 利用 Externalizable 实现序列化

  • 博客分类:
  • Java

   如果我们想利用网络传输对象或者将对象内部状态持久化,那么该对象必须是可序列化的。

    类的可序列化很简单,只需实现serializable或者externalizable接口即可。

    通常情况下,如果一个类仅仅实现了serializable接口,序列化框架为我们提供了一个默认的序列化形式。

如果想要自定义序列化形式,就要考虑添加readObject方法和对应的writeObject方法。详细方法签名如下:

Java代码 复制代码 收藏代码
  1. private void readObject() throws ClassNotFoundException,IOException 
  2. private void writeObject() throws IOException 
private void readObject() throws ClassNotFoundException,IOException
private void writeObject() throws IOException

以上方法并不是Object的方法,该方法的调用是利用反射机制实现的。另外除了以上两个方法以外,还有另外三个于序列化有关的方法:

Java代码 复制代码 收藏代码
  1. ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException; 
  2. NY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException; 
  3. private void readObjectNoData() 
  4.     throws ObjectStreamException; 
 ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
 private void readObjectNoData()
     throws ObjectStreamException;

   除了实现serialiable接口以外,还可以实现externalizable接口。该接口继承自serializable接口,并且包含了两个抽象方法:

Java代码 复制代码 收藏代码
  1. void writeExternal(ObjectOutput out) throws IOException; 
  2. void readExternal(ObjectInput in) throws IOException, ClassNotFoundException; 
    void writeExternal(ObjectOutput out) throws IOException;
    void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;

     因此,实现externalizable接口的类的序列化必须由该类自身来提供,具体的实现分别写在抽象方法中。

     通过调用writeExternal方法实现对象的序列化

     通过调用readExternal方法实现对象的反序列化(该方法调用之前会调用可序列化类的默认构造方法,因此该类必须提供一个默认的构造方法,这也是和serializable接口的一个区别。不提供默认构造方法会抛出no

valid constructor错误信息。)

    下面提供一个简单的例子:

    Student是实现了Externalizable接口的可序列化类,并且显式提供了一个默认构造方法。

Java代码 复制代码 收藏代码
  1. public class Student implements Externalizable{ 
  2.  
  3.     private String name; 
  4.      
  5.     private String password; 
  6.      
  7.     public Student(String name ,String password) { 
  8.         this.name     = name; 
  9.         this.password = password; 
  10.     } 
  11.      
  12.     public Student() { 
  13.         System.out.println("default constructor is called"); 
  14.     } 
  15.     public String getName() { 
  16.         return name; 
  17.     } 
  18.      
  19.     public String getPassword() { 
  20.         return password; 
  21.     } 
  22.  
  23.     @Override 
  24.     public void writeExternal(ObjectOutput out) throws IOException { 
  25.         System.out.println("writeExternal method is called"); 
  26.         out.writeObject(name);      // write name 
  27.         /* encrypt password */ 
  28.         out.writeObject("***" + password);  // write password 
  29.     } 
  30.  
  31.     @Override 
  32.     public void readExternal(ObjectInput in) throws IOException, 
  33.             ClassNotFoundException { 
  34.         System.out.println("readExternal method is called"); 
  35.         /* read in order */ 
  36.         this.name = (String)in.readObject(); 
  37.         this.password = ((String)in.readObject()).substring(3); // decrypt password 
  38.     } 
  39.      
  40.     @Override 
  41.     public String toString() { 
  42.         return "name:" + name + ",password:" + password; 
  43.     } 
  44.  
public class Student implements Externalizable{

	private String name;
	
	private String password;
	
	public Student(String name ,String password) {
		this.name     = name;
		this.password = password;
	}
	
	public Student() {
		System.out.println("default constructor is called");
	}
	public String getName() {
		return name;
	}
	
	public String getPassword() {
		return password;
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		System.out.println("writeExternal method is called");
		out.writeObject(name);		// write name
		/* encrypt password */
		out.writeObject("***" + password);  // write password
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		System.out.println("readExternal method is called");
		/* read in order */
		this.name = (String)in.readObject();
		this.password = ((String)in.readObject()).substring(3);	// decrypt password
	}
	
	@Override
	public String toString() {
		return "name:" + name + ",password:" + password;
	}

}

     测试类:

Java代码 复制代码 收藏代码
  1. public class Demo { 
  2.  
  3.     public static void main(String[] args) throws Exception { 
  4.         Student stu = new Student("benson", "21345"); 
  5.         serialize(stu); 
  6.         deSerialize(); 
  7.     } 
  8.  
  9.     private static void serialize(Student stu) throws FileNotFoundException, 
  10.             IOException { 
  11.  
  12.         OutputStream os = new FileOutputStream(new File("user.bin")); 
  13.         ObjectOutputStream oos = new ObjectOutputStream(os); 
  14.         oos.writeObject(stu); 
  15.         oos.close(); 
  16.  
  17.     } 
  18.  
  19.     private static void deSerialize() throws FileNotFoundException, 
  20.             IOException, ClassNotFoundException { 
  21.  
  22.         InputStream is = new FileInputStream(new File("user.bin")); 
  23.         ObjectInputStream ois = new ObjectInputStream(is); 
  24.         Student user = (Student) ois.readObject(); 
  25.         ois.close(); 
  26.         System.out.println(user); 
  27.  
  28.     } 
public class Demo {

	public static void main(String[] args) throws Exception {
		Student stu = new Student("benson", "21345");
		serialize(stu);
		deSerialize();
	}

	private static void serialize(Student stu) throws FileNotFoundException,
			IOException {

		OutputStream os = new FileOutputStream(new File("user.bin"));
		ObjectOutputStream oos = new ObjectOutputStream(os);
		oos.writeObject(stu);
		oos.close();

	}

	private static void deSerialize() throws FileNotFoundException,
			IOException, ClassNotFoundException {

		InputStream is = new FileInputStream(new File("user.bin"));
		ObjectInputStream ois = new ObjectInputStream(is);
		Student user = (Student) ois.readObject();
		ois.close();
		System.out.println(user);

	}
}

   Student在序列化的时候对密码字段进行了简单的加密,反序列化时进行了解密。

    打印结果如下:

Command代码 复制代码 收藏代码
  1. writeExternal method is called 
  2. default constructor is called 
  3. readExternal method is called 
  4. name:benson,password:21345 

    假如在网络上传输该对象的二进制时被黑客截获,虽然他可以利用公开的序列化格式对截获的信息进行分析,但是密码加过密,因此加大了黑客解密的难度。如该文件内容:

user.bin:



  • 大小: 18.8 KB
3
0
分享到:     
评论
1 楼    zhoujianyong2011    1 小时前           
不过现在用这个来实现序列化的不多了,目前hessian和kryo两者不错,效率快
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值