Serializing java.util.Date

前端 未结 2 1068
春和景丽
春和景丽 2020-12-19 03:07

Does anyone know how a java.util.Date gets serialized? I mean explain to me exactly what each byte is? I tried writing out a long then a date and I can see matches but the

相关标签:
2条回答
  • 2020-12-19 03:22

    The details of the format of Java object serialisation are specified in Java Object Serialization Specification. Other than magic and version numbers , details of the Date class and the fact the object is a Date is written to the stream.

    The API doc for Date serialised form is:

    The value returned by getTime() is emitted (long). This represents the offset from January 1, 1970, 00:00:00 GMT in milliseconds.

    Note that it actually breaks the spec by not calling defaultWriteObject or putFields.

    0 讨论(0)
  • 2020-12-19 03:32
    /**
     * Save the state of this object to a stream (i.e., serialize it).
     *
     * @serialData The value returned by <code>getTime()</code>
     *         is emitted (long).  This represents the offset from
     *             January 1, 1970, 00:00:00 GMT in milliseconds.
     */
    private void writeObject(ObjectOutputStream s)
         throws IOException
    {
        s.writeLong(getTimeImpl());
    }
    

    therefore, it's the long value representing the offset from Jan 1 1970 00:00:00 GMT in milliseconds.

    EDIT: however this is preceeded and succeeded by some headers:

    0x73 - being the code for an ordinary object (TC_OBJECT)    
    0x72 - being the code for a class description (TC_CLASSDESC)    
    "java.util.Date" - the name of the class    
    7523967970034938905L - the serialVersionUID    
    0|0x02|0x01 - flags including SC_SERIALIZABLE & SC_WRITE_METHOD    
    0 - number of fields    
    0x78 - TC_ENDBLOCKDATA    
    null - there is no superclass descriptor    
    the time (long milliseconds since epoch)    
    0x78 - TC_ENDBLOCKDATA
    
    0 讨论(0)
提交回复
热议问题