Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4510303

Serialization of SimpleDateFormat is incompatible

    XMLWordPrintable

Details

    • sparc
    • solaris_2.6

    Description



      Name: ooR10001 Date: 10/03/2001


      Serialization of SimpleDateFormat is incompatible with any other implementation because
      serialization puts private class sun.util.calendar.ZoneInfo into the serialized stream.
      Such stream could not be deserialized under any other implementation which does not have such
      private class.

      Following sample test is used to create serial stream under JDK1.4:

      ---------- test1.java ---------
      import java.text.SimpleDateFormat;
      import java.util.TimeZone;
      import java.util.GregorianCalendar;
      import java.util.Locale;
      import java.io.FileOutputStream;
      import java.io.ObjectOutputStream;

      public class test1 {

        public static void main(String[] args) {
          SimpleDateFormat sdf =
              new SimpleDateFormat("yyyy.MM.dd 'e' hh.mm.ss zzz", Locale.ENGLISH);
          sdf.setCalendar(new GregorianCalendar(TimeZone.getTimeZone("GMT"),
                                                Locale.ENGLISH));
          try {
              FileOutputStream fos = new FileOutputStream("test.ser");
              ObjectOutputStream oos = new ObjectOutputStream(fos);
              oos.writeObject(sdf);
          } catch (java.io.IOException e) {
            System.out.println("Exception thrown: " + e);
          }
        }

      }
      -------------------------------

      Commands below are used to make test.ser file under jdk1.4-beta2-b81:
      ---------------------------------
      % java -version

      java version "1.4.0-beta3"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b81)
      Java HotSpot(TM) Client VM (build 1.4.0-beta3-b81, mixed mode)

      % java test1

      ---------------------------------

      Following test demonstrates that test.ser is incompatible with JDK1.3:

      ------------ test2.java -----------------
      import java.io.FileInputStream;
      import java.io.ObjectInputStream;

      public class test2 {

        public static void main(String[] args) {
          try {
              FileInputStream fis = new FileInputStream("test.ser");
              ObjectInputStream ois = new ObjectInputStream(fis);
              Object obj = ois.readObject();
          } catch (java.io.IOException e) {
            System.out.println("Exception thrown: " + e);
          } catch (ClassNotFoundException e) {
            System.out.println("Exception thrown: " + e);
          }
        }

      }
      -----------------------------------------

      Commands below are use to run this test under JDK1.3:

      -----------------------------------------
      % java -version

      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, interpreted mode)

      % java test2

      Exception thrown: java.lang.ClassNotFoundException: sun.util.calendar.ZoneInfo
      -----------------------------------------

      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              okutsu Masayoshi Okutsu
              oovsunw Oov Oov (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: