Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

serialization - Serializing java.util.Date

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 there are other characters that I just don't get.

Our application makes server requests with data which means it gets serialized from client to server. The team that does stress testing uses a tool that captures these requests and modifies them, the problem is they want to process dates and I don't know how to interpret the byte stream. The dude I am talking to seems willing to learn but so far I haven't found anything that I understand to point him to...

Code I used:

  FileOutputStream fos = null;
  ObjectOutputStream oos = null;
  try
  {
   fos = new FileOutputStream("t.tmp");
   oos = new ObjectOutputStream(fos);

   Date today = new Date();

   oos.writeLong(today.getTime());
   oos.writeObject("Today");
   oos.writeObject(today);

   oos.close();
  }
  catch(FileNotFoundException e)
  {
   e.printStackTrace();
  }
  catch(IOException e)
  {
   e.printStackTrace();
  }

EDIT:

The output from the above is:

"?í w  ,áq?-t Todaysr java.util.DatehjKYt  xpw  ,áq?-x"

The long is "w ,áq?-" so what is the stuff between the long and the Date object, i.e. "hjKYt xp"

NOTE some the blanks are unprintable characters NULL, SOH, backspace etc. I understand that it is the hex value that matters.

EDIT:

Still having problems. For some reason the serialized HTTP request does not serialize the date exactly like the answer I accepted says. Very close but still different and I don't know why. What's even odder is that when I simply serialize a date it seems to work fine. FYI at worj we use Websphere 6.1 Here are some examples of what is being sent in the request:

 lr_start_transaction("20000101");
\x0Ejava.util.Datehj\x81\x01KYt\x19\x03\x00\x00xpw\x08\x00\x00\x01,\xE10\x0BXxt\x00\x08

 lr_start_transaction("20000102");
\x0Ejava.util.Datehj\x81\x01KYt\x19\x03\x00\x00xpw\x08\x00\x00\x01,\xE10>\x9Dxt\x00\x08

 lr_start_transaction("20000103");
\x0Ejava.util.Datehj\x81\x01KYt\x19\x03\x00\x00xpw\x08\x00\x00\x01,\xE10z\xDBxt\x00\x08

I have been able to identify most fields but not the actual time! E.g the serialVersionUID is hj\x81\x01KYt\x19

EDIT (FINAL):

I found the date but it was no where near where I expected it! It was well after the sample I had because other data fields were appearing I thought the date was done - it was just fluke that I noticed the hex pattern of the date I was looking for! Example:

 lr_start_transaction("20000101");
\x0Ejava.util.Datehj\x81\x01KYt\x19\x03\x00\x00xpw\x08\x00\x00\x01,\xE10\x0BXxt\x00\x08OTTST153t\x00\x06/Web2/t\x00\x044971t\x00\x0B12ce12f737d\x00\x00\x01,\xE10\x0BXsq\x00~\x00\x0Fw\x08\x00\x00\x00\xDCk\xE2T\x80xt

The date value is right at the very end!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...