Movatterモバイル変換


[0]ホーム

URL:


CONTENTS |PREV |NEXTJava Object Serialization Specification
version 6.0

Object Serialization Stream Protocol






CHAPTER 6

Topics:


6.1 Overview

The stream format satisfies the following design goals:


6.2 Stream Elements

A basic structure is neededto represent objects in a stream. Each attribute of the objectneeds to be represented: its classes, its fields, and data writtenand later read by class-specific methods. The representation ofobjects in the stream can be described with a grammar. There arespecial representations for null objects, new objects, classes,arrays, strings, and back references to any object already in thestream. Each object written to the stream is assigned a handle thatis used to refer back to the object. Handles are assignedsequentially starting from 0x7E0000. The handles restart at0x7E0000 when the stream is reset.

A class object is represented by the following:

AnObjectStreamClass object for a Class that is nota dynamic proxy class is represented by the following:

AnObjectStreamClass object for a dynamic proxyclass is represented by the following:

The representation ofString objects consists of length information followedby the contents of the string encoded in modified UTF-8. Themodified UTF-8 encoding is the same as used in the Java VirtualMachine and in thejava.io.DataInput andDataOutput interfaces; it differs from standard UTF-8in the representation of supplementary characters and of the nullcharacter. The form of the length information depends on the lengthof the string in modified UTF-8 encoding. If the modified UTF-8encoding of the givenString is less than 65536 bytesin length, the length is written as 2 bytes representing anunsigned 16-bit integer. Starting with the Java 2 platform,Standard Edition, v1.3, if the length of the string in modifiedUTF-8 encoding is 65536 bytes or more, the length is written in 8bytes representing a signed 64-bit integer. The typecode precedingtheString in the serialization stream indicates whichformat was used to write theString.

Arrays are represented by the following:

Enum constants are represented by the following:

New objects in the stream are represented by the following:

All primitive data written by classes is buffered and wrapped inblock-data records, regardless if the data is written to the streamwithin awriteObject method or written directly to thestream from outside awriteObject method. This datacan only be read by the correspondingreadObjectmethods or be read directly from the stream. Objects written by thewriteObject method terminate any previous block-datarecord and are written either as regular objects or null or backreferences, as appropriate. The block-data records allow errorrecovery to discard any optional data. When called from within aclass, the stream can discard any data or objects until theendBlockData.


6.3 Stream ProtocolVersions

It was necessary to makea change to the serialization stream format in JDK 1.2 that is notbackwards compatible to all minor releases of JDK 1.1. To providefor cases where backwards compatibility is required, a capabilityhas been added to indicate whatPROTOCOL_VERSION to usewhen writing a serialization stream. The methodObjectOutputStream.useProtocolVersion takes as aparameter the protocol version to use to write the serializationstream.

The Stream Protocol Versions are as follows:

JDK 1.2 defaults towritingPROTOCOL_VERSION_2.

JDK 1.1 defaults towritingPROTOCOL_VERSION_1.

JDK 1.1.7 and greatercan read both versions.

Releases prior to JDK 1.1.7 can only readPROTOCOL_VERSION_1.


6.4 Grammar for the StreamFormat

The table below contains the grammar for the stream format.Nonterminal symbols are shown in italics. Terminal symbols in afixed width font. Definitions of nonterminals are followedby a ":". The definition is followed by one or morealternatives, each on a separate line. The following tabledescribes the notation:

Notation
Meaning

(datatype)

This token has the data type specified, such as byte.

token[n]

A predefined number of occurrences of the token, that is anarray.

x0001

A literal value expressed in hexadecimal. The number of hexdigits reflects the size of the value.

<xxx>

A value read from the stream used to indicate the length of anarray.

Note that the symbol (utf) is used to designate a string writtenusing 2-byte length information, and (long-utf) is used todesignate a string written using 8-byte length information. Fordetails, refer toSection 6.2,"Stream Elements".

6.4.1 Rules ofthe Grammar

A Serialized stream is represented by any stream satisfying thestream rule.

stream:magic version contents
contents:contentcontents content
content:objectblockdata
object:newObjectnewClassnewArraynewString  newEnumnewClassDescprevObjectnullReferenceexceptionTC_RESET
newClass:TC_CLASS classDesc newHandle
classDesc:  newClassDesc  nullReference  (ClassDesc)prevObject      // an object required to be of type                             // ClassDesc
superClassDesc:  classDesc
newClassDesc:TC_CLASSDESC className serialVersionUID newHandle classDescInfoTC_PROXYCLASSDESC newHandle proxyClassDescInfo
classDescInfo:  classDescFlags fields classAnnotation superClassDesc
className:  (utf)
serialVersionUID:  (long)
classDescFlags:  (byte)                  // Defined in Terminal Symbols and                            // Constants
proxyClassDescInfo:  (int)<count> proxyInterfaceName[count] classAnnotation      superClassDesc
proxyInterfaceName:
  (utf)
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,                             // in field descriptor format
classAnnotation:  endBlockData  contents endBlockData      // contents written by annotateClass
prim_typecode:  `B'       // byte  `C'       // char  `D'       // double  `F'       // float  `I'       // integer  `J'       // long  `S'       // short  `Z'       // boolean
obj_typecode:  `[`   // array  `L'       // object
newArray:TC_ARRAY classDesc newHandle (int)<size> values[size]
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 writeExternalPROTOCOL_VERSION_2.
blockdata:  blockdatashort  blockdatalong
blockdatashort:TC_BLOCKDATA (unsigned byte)<size> (byte)[size]
blockdatalong:TC_BLOCKDATALONG (int)<size> (byte)[size]
endBlockData   :TC_ENDBLOCKDATA
externalContent:          // Only parseable by readExternal  ( bytes)                // primitive data    object
externalContents:         // externalContent written by   externalContent         // writeExternal inPROTOCOL_VERSION_1.  externalContents externalContent
newString:TC_STRING newHandle (utf)TC_LONGSTRING newHandle (long-utf)
newEnum:  TC_ENUM classDesc newHandle enumConstantName
enumConstantName:  (String)object
prevObjectTC_REFERENCE (int)handle
nullReferenceTC_NULL
exception:TC_EXCEPTION reset (Throwable)object         reset
magic:STREAM_MAGIC
versionSTREAM_VERSION
values:          // The size and types are described by the                 // classDesc for the current object
newHandle:       // The next number in sequence is assigned                 // to the object being serialized or deserialized
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

6.4.2 TerminalSymbols and Constants

The following symbols injava.io.ObjectStreamConstants define the terminal andconstant values expected in a stream.

    final static short STREAM_MAGIC = (short)0xaced;    final static short STREAM_VERSION = 5;    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  int   baseWireHandle = 0x7E0000;
The flag byteclassDescFlags may include values of

    final static byte SC_WRITE_METHOD = 0x01; //if SC_SERIALIZABLE     final static byte SC_BLOCK_DATA = 0x08;    //if SC_EXTERNALIZABLE    final static byte SC_SERIALIZABLE = 0x02;    final static byte SC_EXTERNALIZABLE = 0x04;    final static byte SC_ENUM = 0x10;
The flagSC_WRITE_METHOD is set if the Serializable class writingthe stream had awriteObject method that may havewritten additional data to the stream. In this case aTC_ENDBLOCKDATA marker is always expected to terminate thedata for that class.

The flagSC_BLOCKDATA is set if theExternalizableclass is written into the stream usingSTREAM_PROTOCOL_2. By default, this is the protocolused to writeExternalizable objects into the streamin JDK 1.2. JDK 1.1 writesSTREAM_PROTOCOL_1.

The flagSC_SERIALIZABLE is set if the class that wrote the streamextendedjava.io.Serializable but notjava.io.Externalizable, the class reading the streammust also extendjava.io.Serializable and the defaultserialization mechanism is to be used.

The flagSC_EXTERNALIZABLE is set if the class that wrote thestream extendedjava.io.Externalizable, the classreading the data must also extendExternalizable andthe data will be read using itswriteExternal andreadExternal methods.

The flagSC_ENUM is set if the class that wrote the stream was anenum type. The receiver's corresponding class must also be anenum type. Data for constants of the enum type will be written andread as described inSection 1.12, "Serialization ofEnum Constants".


Example

Consider the case of anoriginal class and two instances in a linked list:

class List implements java.io.Serializable {    int value;    List next;    public static void main(String[] args) {        try {            List list1 = new List();            List list2 = new List();            list1.value = 17;            list1.next = list2;            list2.value = 19;            list2.next = null;            ByteArrayOutputStream o = new ByteArrayOutputStream();            ObjectOutputStream out = new ObjectOutputStream(o);            out.writeObject(list1);            out.writeObject(list2);            out.flush();            ...        } catch (Exception ex) {            ex.printStackTrace();        }    }}
The resulting streamcontains:

00: ac ed 00 05 73 72 00 04 4c 69 73 74 69 c8 8a 15 >....sr..Listi...<
10: 40 16 ae 68 02 00 02 49 00 05 76 61 6c 75 65 4c >Z......I..valueL<
20: 00 04 6e 65 78 74 74 00 06 4c 4c 69 73 74 3b 78 >..nextt..LList;x<
30: 70 00 00 00 11 73 71 00 7e 00 00 00 00 00 13 70 >p....sq.~......p<
40: 71 00 7e 00 03                                  >q.~..<


CONTENTS |PREV |NEXT

[8]ページ先頭

©2009-2026 Movatter.jp