спецификации, руководства, описания, API
|CONTENTS | PREV | NEXT||Java Object Serialization Specification|
Each versioned class must identify the original class version for which it is capable of writing streams and from which it can read. For example, a versioned class must declare:
private static final long serialVersionUID = 3487495895819393L;The stream-unique identifier is a 64-bit hash of the class name, interface class names, methods, and fields. The value must be declared in all versions of a class except the first. It may be declared in the original class bu t is not required. The value is fixed for all compatible classes. If the SUID is not declared for a class, the value defaults to the hash for that class.
Serializableclasses do not need to anticipate versioning; however,
Note - It is strongly recommended that serializable classes that are inner classes or which contain inner classes declare the serialVersionUID data member. This is because different implementations of compilers could use differen t names for synthetic members that are generated for the implementation of inner classes, and these names are used in the current computation of SUIDs.
The initial version of an
Externalizableclass must output a stream data format that is extensible in the future. The initial version of the method
readExternalhas to be able to read the output format of all future versions of t he method
serialVersionUIDis computed using the signature of a stream of bytes that reflect the class definition. The National Institute of Standards and Technology (NIST) Secure Hash Algorithm (SHA-1) is used to compute a signature for the stream . The first two 32-bit quantities are used to form a 64-bit hash. A
java.lang.DataOutputStreamis used to convert primitive data types to a sequence of bytes. The values input to the stream are defined by the JavaTM Virtual Machine (VM) specification for classes.
The sequence of items in the stream is as follows:
<clinit>, in UTF encoding.
()V, in UTF encoding.
<init>, in UTF encoding.
sha, the hash value would be computed as follows: long hash = ((sha >>> 24) & 0xFF) | ((sha >>> 16) & 0xFF) << 8 | ((sha >>> 8) & 0xFF) << 16 | ((sha >>> 0) & 0xFF) << 24 | ((sha >>> 24) & 0xFF) << 32 | ((sha >>> 16) & 0xFF) << 40 | ((sha >>> 8) & 0xFF) << 48 | ((sha >>> 0) & 0xFF) << 56;