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

java.util.Locale is incorrectly deserialized

    Details

      Description



      Name: avC70361 Date: 02/11/98



        The java.util.Locale deserialization is incorrectly performed. This is due to
      a chage in Locale code for jdk1.2 and jdk 1.1.6(the field int hashcode made
      transient). This change makes the field be set to 0 when the deserialization is
      performed. Therefore the hashCode result is also 0. But non-serialized instance
      has other hashCode value. So, method equals()(which using hashCode()) returns
      false for instances before and after serialization/desarialization.

      Here is the test demonstrating the bug.
      ----------LocaleTest.java----------
      import java.util.Locale;
      import java.io.IOException;
      import java.io.ByteArrayInputStream;
      import java.io.ByteArrayOutputStream;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;

      public class LocaleTest {

        public static void main(String args[]) {
          Locale locale1 = null;
          Locale locale2 = null;

          ObjectOutputStream ostream = null;
          ByteArrayOutputStream baos = null;
          ObjectInputStream istream = null;

          try {

            ostream = new ObjectOutputStream(
              baos = new ByteArrayOutputStream()
            );

          } catch(IOException e) {
            System.out.println("Unexpected exception : " + e);
            System.exit(1);
          }

          try {

            locale1 = Locale.US;

            ostream.writeObject(locale1);
            ostream.flush();

            byte bytes[] = baos.toByteArray();

            istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
            locale2 = (Locale)istream.readObject();

          } catch(Exception e) {
            System.out.println("Unexpected exception : " + e);
          }

          if (!locale1.equals(locale2)) {
            System.out.println("Locales before and after serialization are not equal\nlocale1 = " + locale1 + "(hash code = " + locale1.hashCode() + ")\nlocale2 = " + locale2 + "(hash code = " + locale2.hashCode() + ")");
          } else {
            System.out.println("OK");
          }
        }

      }
      -----------------The test output----------
      > java LocaleTest
      Locales before and after serialization are not equal
      locale1 = en_US(hash code = 1591)
      locale2 = en_US(hash code = 0)

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

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                rgillamsunw Richard Gillam (Inactive)
                Reporter:
                ovlasov Oleksandr Vlasov (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: