手机版
你好,游客 登录 注册
背景:
阅读新闻

Java的序列化机制原理分析

[日期:2012-06-02] 来源:Linux社区  作者:zhongweijian [字体:大 中 小]
然后我们先看看writeClassDesc(desc, false)的实现:
  1. private void writeClassDesc(ObjectStreamClass desc, boolean unshared)   
  2.       throws IOException   
  3.   {  
  4.       int handle;  
  5.       if (desc == null) {  
  6.           writeNull();  
  7.       } else if (!unshared && (handle = handles.lookup(desc)) != -1) {  
  8.           writeHandle(handle);  
  9.       } else if (desc.isProxy()) { //如果是proxy对象,则调用该序列化机制 ,isProxy的判断   
  10.          //isProxy = Proxy.isProxyClass(cl); Returns true if and only if the specified class was dynamically generated to be a proxy class using the getProxyClass method or the newProxyInstance method.    
  11.           writeProxyDesc(desc, unshared);  
  12.       } else {  
  13.           writeNonProxyDesc(desc, unshared);  
  14.       }  
  15.   }  
  16.     
  17.   /** 
  18.    * Writes class descriptor representing a dynamic proxy class to stream. 
  19.    */  
  20.   private void writeProxyDesc(ObjectStreamClass desc, boolean unshared)   
  21.       throws IOException   
  22.   {  
  23.       bout.writeByte(TC_PROXYCLASSDESC); //写入代理对象的标示符   
  24.       handles.assign(unshared ? null : desc);  
  25.   
  26.       Class cl = desc.forClass();  
  27.       Class[] ifaces = cl.getInterfaces(); //如果是proxy对象,则写入对象的interfaces的名称   
  28.       bout.writeInt(ifaces.length); //先写入interface个数   
  29.       for (int i = 0; i < ifaces.length; i++) {  
  30.           bout.writeUTF(ifaces[i].getName()); //再写入每个interface的名称   
  31.       }  
  32.         
  33.       bout.setBlockDataMode(true);  
  34.       annotateProxyClass(cl);  
  35.       bout.setBlockDataMode(false);  
  36.       bout.writeByte(TC_ENDBLOCKDATA); //结束标签   
  37.         
  38.       writeClassDesc(desc.getSuperDesc(), false);//递归写入父类的序列化信息,因为Java是单继承,   
  39.   }  
  40.     
  41.   /** 
  42.    * Writes class descriptor representing a standard (i.e., not a dynamic 
  43.    * proxy) class to stream. 
  44.    */  
  45.   private void writeNonProxyDesc(ObjectStreamClass desc, boolean unshared)   
  46.       throws IOException   
  47.   {  
  48.       bout.writeByte(TC_CLASSDESC);//写入class对象的标示符   
  49.       handles.assign(unshared ? null : desc);  
  50.         
  51.       if (protocol == PROTOCOL_VERSION_1) { //如果非代理类对象的具体class信息,查看下面的方法   
  52.           // do not invoke class descriptor write hook with old protocol   
  53.           desc.writeNonProxy(this);  
  54.       } else {  
  55.           writeClassDescriptor(desc);   
  56.       }  
  57.         
  58.       Class cl = desc.forClass();  
  59.       bout.setBlockDataMode(true);  
  60.       annotateClass(cl);  
  61.       bout.setBlockDataMode(false);  
  62.       bout.writeByte(TC_ENDBLOCKDATA);  
  63.         
  64.       writeClassDesc(desc.getSuperDesc(), false);//递归写入父类的序列化信息,因为java是单继承,   
  65.   }  
writeClassDescriptor(desc)方法:
  1.        throws IOException  
  2.    {  
  3.        desc.writeNonProxy(this);  
  4.    }  
  5.   
  6. void writeNonProxy(ObjectOutputStream out) throws IOException {  
  7.        out.writeUTF(name);//写入类的名称   
  8.        out.writeLong(getSerialVersionUID());//写入类的SerialVersionUID   
  9.   
  10.        byte flags = 0;  
  11.        if (externalizable) {//是否实现externalizable接口   
  12.            flags |= ObjectStreamConstants.SC_EXTERNALIZABLE;  
  13.            int protocol = out.getProtocolVersion();  
  14.            if (protocol != ObjectStreamConstants.PROTOCOL_VERSION_1) {  
  15.                flags |= ObjectStreamConstants.SC_BLOCK_DATA;  
  16.            }  
  17.        } else if (serializable) {//是否实现serializable接口   
  18.            flags |= ObjectStreamConstants.SC_SERIALIZABLE;  
  19.        }  
  20.        if (hasWriteObjectData) {//是否有自定义的重写序列化方法   
  21.            flags |= ObjectStreamConstants.SC_WRITE_METHOD;  
  22.        }  
  23.        if (isEnum) {  
  24.            flags |= ObjectStreamConstants.SC_ENUM;//是否是枚举   
  25.        }  
  26.        out.writeByte(flags);  
  27.          
  28.        out.writeShort(fields.length); //遍历写入各个类的各个field字段类型名称等信息   
  29.        for (int i = 0; i < fields.length; i++) {  
  30.            ObjectStreamField f = fields[i];  
  31.            out.writeByte(f.getTypeCode()); //typecode参考ObjectStreamField.java类   
  32.            out.writeUTF(f.getName());  
  33.            if (!f.isPrimitive()) {  
  34.                out.writeTypeString(f.getTypeString());  
  35.            }  
  36.        }  
  37.    }  
linux
相关资讯       Java教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款