java序列化规范

java序列化:java对象转换成字节流,需要的时候能从字节流得到java对象

序列化后字节流应该遵循的格式

stream:

    magic version contents

magic:

    STREAM_MAGIC

version:

    STREAM_VERSION

contents:

    content
    contents content

content:

    object
    blockdata

object:

    newObject
    newClass
    newArray
    newString
    newClassDesc
    prevObject
    nullReference
    exception
   reset

newClass:

   TC_CLASS classDesc newHandle

classDesc:

    newClassDesc
    nullReference
    (ClassDesc)prevObject // an object required to be of type ClassDesc

newClassDesc:

    TC_CLASSDESC className serialVersionUID newHandle classDescInfo

    TC_PROXYCLASSDESC newHandle proxyClassDescInfo

className:

    (utf)

serialVersionUID:

   (long)

newHandle: //The next number in sequence is assigned to the object being serialized or deserialized

classDescInfo:

    classDescFlags fields classAnnotation superClassDesc

proxyClassDescInfo:
    (int)<count>proxyInterfaceName[count] classAnnotation
    superClassDesc

proxyInterfaceName:
    (utf)

classDescFlags:

    (byte) // Defined in Terminal Symbols and Constants

fields:

    (short)<count> fieldDesc[count]

fieldDesc:

    primitiveDesc
    objectDesc

primitiveDesc:

     prim_typecode fieldName

objectDesc:

    obj_typecode fieldName className1

fieldName:

    (utf)

className1:

    (String)object // String containing the field’s type

superClassDesc:

    classDesc

classAnnotation:

    endBlockData
    contents endBlockData // contents written by annotateClass

prim_typecode:

    `B' // byte
    `C' // char
    `D' // double
    `F' // float
    `I' // int
    `J' // long
    `S' // short
    `Z' // boolean

obj_typecode:

    `[` // array
    `L' // object

newArray:

    TC_ARRAY classDesc newHandle (int)<size> values[size]

values: //The size and types are described by the classDesc for the current object

newObject:

    TC_OBJECT classDesc newHandle classdata[] // data for each class

classdata:

    nowrclass // SC_SERIALIZABLE & classDescFlag && !(SC_WRITE_METHOD & classDescFlags)
    wrclass objectAnnotation // SC_SERIALIZABLE & classDescFlag && SC_WRITE_METHOD &
classDescFlags
    externalContents // SC_EXTERNALIZABLE & classDescFlag && !(SC_BLOCKDATA &
classDescFlags
    objectAnnotation // SC_EXTERNALIZABLE & classDescFlag&& SC_BLOCKDATA & classDescFlags

nowrclass:

    values // fields in order of class descriptor

wrclass:

   nowrclass

objectAnnotation:

   endBlockData
   contents endBlockData // contents written by writeObject or writeExternal PROTOCOL_VERSION_2

externalContents: // externalContent written by

    externalContent // writeExternal in PROTOCOL_VERSION_1
    externalContents externalContent

externalContent: // Only parseable by readExternal

    (bytes)object

blockdata:

    blockdatashort
    blockdatalong

blockdatashort:

    TC_BLOCKDATA (unsigned byte)<size> (byte)[size]

blockdatalong:

    TC_BLOCKDATALONG (int)<size> (byte)[size]

endBlockData:

    TC_ENDBLOCKDATA

newString:

    TC_STRING newHandle (utf)

prevObject:

    TC_REFERENCE (int)handle

nullReference:

    TC_NULL

exception:

    TC_EXCEPTION reset(Throwable) object reset

reset: // The set of known objects is discarded so the objects of the exception do not overlap with the previously sent objects or with objects that may be sent after the exception

 

字节流中特殊字节的意义(在java.io.ObjectStreamConstants)

final static short STREAM_MAGIC = (short)0xaced;
final static short STREAM_VERSION = 5;

final static byte TC_BASE = 0x70;
final static byte TC_NULL = (byte)0x70;
final static byte TC_REFERENCE = (byte)0x71;
final static byte TC_CLASSDESC = (byte)0x72;
final static byte TC_OBJECT = (byte)0x73;
final static byte TC_STRING = (byte)0x74;
final static byte TC_ARRAY = (byte)0x75;
final static byte TC_CLASS = (byte)0x76;
final static byte TC_BLOCKDATA = (byte)0x77;
final static byte TC_ENDBLOCKDATA = (byte)0x78;
final static byte TC_RESET = (byte)0x79;
final static byte TC_BLOCKDATALONG = (byte)0x7A;
final static byte TC_EXCEPTION = (byte)0x7B;
final static byte TC_LONGSTRING = (byte) 0x7C;
final static byte TC_PROXYCLASSDESC = (byte) 0x7D;

final static byte TC_ENUM = (byte)0x7E;

final static byte TC_MAX = (byte)0x7E;
final static int baseWireHandle = 0x7E0000;

final static byte SC_WRITE_METHOD = 0x01;
final static byte SC_BLOCK_DATA = 0x08;

final static byte SC_SERIALIZABLE = 0x02;
final static byte SC_EXTERNALIZABLE = 0x04;

final static byte SC_ENUM = 0x10;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值