(original) (raw)
ObjectStreamConstants.PROTOCOL_VERSION_2
Indicates the new external data format. Primitive data is written in block data mode and is terminated with TC_ENDBLOCKDATA.
Block data boundaries have been standardized. Primitive data written in block data mode is normalized to not exceed 1024 byte chunks. The benefit of this change was to tighten the specification of serialized data format within the stream. This change is fully backward and forward compatible.
JDKTM 1.2 defaults to writing PROTOCOL_VERSION_2
.
JDKTM 1.1 defaults to writing PROTOCOL_VERSION_1
.
JDKTM 1.1.7 and greater can read both versions.
Releases prior to JDKTM 1.1.7 can only read PROTOCOL_VERSION_1
.
6.4 Grammar for the Stream Format
The table below contains the grammar for the stream format. Nonterminal symbols are shown in italics. Terminal symbols in a fixed width font. Definitions of nonterminals are followed by a ":". The definition is followed by one or more alternatives, each on a separate line. The following table describes 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 an array. |
x0001 | A literal value expressed in hexadecimal. The number of hex digits reflects the size of the value. |
<_xxx_> | A value read from the stream used to indicate the length of an array. |
Note that the symbol (utf) is used to designate a string written using 2-byte length information, and (long-utf) is used to designate a string written using 8-byte length information. For details, refer to Section6.2 Stream Elements".
6.4.1 Rules of the Grammar
A Serialized stream is represented by any stream satisfying the stream rule.
stream:
magic version contents
contents:
content
contents content
content:
object
blockdata
object:
newObject
newClass
newArray
newString
newEnum
newClassDesc
prevObject
nullReference
exception
TCRESET
newClass:
TCCLASS classDesc newHandle
classDesc:
newClassDesc
nullReference
(ClassDesc)prevObject // an object required to be of type
// ClassDesc
superClassDesc:
classDesc
newClassDesc:
TCCLASSDESC className serialVersionUID newHandle classDescInfo
TCPROXYCLASSDESC newHandle proxyClassDescInfo
classDescInfo:
classDescFlags fields classAnnotation superClassDesc
className:
(utf)
serialVersionUID:
(long)
classDescFlags:
(byte) // Defined in Terminal Symbols and
// Constants
proxyClassDescInfo:
(int) proxyInterfaceName[count] classAnnotation
superClassDesc
proxyInterfaceName:
(utf)
fields:
(short) fieldDesc[count]
fieldDesc:
primitiveDesc
objectDesc
primitiveDesc:
primtypecode fieldName
objectDesc:
objtypecode 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
primtypecode:
B' // byte_ _
C' // char
D' // double_ _
F' // float
I' // integer_ _
J' // long
S' // short_ _
Z' // boolean
objtypecode:
[
// array
`L' // object
newArray:
TCARRAY classDesc newHandle (int) values[size]
newObject:
TCOBJECT classDesc newHandle classdata[] // data for each class
classdata:
nowrclass // SCSERIALIZABLE & classDescFlag &&
// !( SCWRITEMETHOD & classDescFlags)
wrclass objectAnnotation // SCSERIALIZABLE & classDescFlag &&
// SCWRITEMETHOD & classDescFlags
externalContents // SCEXTERNALIZABLE & classDescFlag &&
// !( SCBLOCKDATA & classDescFlags
objectAnnotation // SCEXTERNALIZABLE & classDescFlag&&
// SCBLOCKDATA & classDescFlags
nowrclass:
values // fields in order of class descriptor
wrclass:
nowrclass
objectAnnotation:
endBlockData
contents endBlockData // contents written by writeObject
// or writeExternal PROTOCOLVERSION2 .
blockdata:
blockdatashort
blockdatalong
blockdatashort:
TCBLOCKDATA (unsigned byte) (byte)[size]
blockdatalong:
TCBLOCKDATALONG (int) (byte)[size]
endBlockData :
TCENDBLOCKDATA
externalContent: // Only parseable by readExternal
( bytes) // primitive data
object
externalContents: // externalContent written by
externalContent // writeExternal in PROTOCOLVERSION1 .
externalContents externalContent
newString:
TCSTRING newHandle (utf)
TCLONGSTRING newHandle (long-utf)
newEnum:
TC_ENUM classDesc newHandle enumConstantName
enumConstantName:
(String)object
prevObject
TCREFERENCE (int)handle
nullReference
TCNULL
exception:
TCEXCEPTION reset (Throwable)object reset
magic:
STREAMMAGIC
version
STREAMVERSION
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 Terminal Symbols and Constants
The following symbols in java.io.ObjectStreamConstants
define the terminal and constant 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 byte classDescFlags 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 flag SC_WRITE_METHOD is set if the Serializable class writing the stream had a writeObject
method that may have written additional data to the stream. In this case a TC_ENDBLOCKDATA marker is always expected to terminate the data for that class.
The flag SC_BLOCKDATA is set if the Externalizable
class is written into the stream using STREAM_PROTOCOL_2
. By default, this is the protocol used to write Externalizable
objects into the stream in JDKTM 1.2. JDKTM 1.1 writes STREAM_PROTOCOL_1.
The flag SC_SERIALIZABLE is set if the class that wrote the stream extended java.io.Serializable
but not java.io.Externalizable
, the class reading the stream must also extend java.io.Serializable
and the default serialization mechanism is to be used.
The flag SC_EXTERNALIZABLE is set if the class that wrote the stream extended java.io.Externalizable
, the class reading the data must also extend Externalizable
and the data will be read using its writeExternal
and readExternal
methods.
The flag SC_ENUM is set if the class that wrote the stream was an enum type. The receiver's corresponding class must also be an enum type. Data for constants of the enum type will be written and read as described in Section1.12 Serialization of Enum Constants".
Example
Consider the case of an original 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 stream contains:
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...<
Copyright © 2004 Oracle and/or its affiliates. All rights reserved