src/share/classes/java/util/Hashtable.java

Print this page

        

@@ -843,29 +843,42 @@
      *             <i>size</i> of the Hashtable (the number of key-value
      *             mappings), followed by the key (Object) and value (Object)
      *             for each key-value mapping represented by the Hashtable
      *             The key-value mappings are emitted in no particular order.
      */
-    private synchronized void writeObject(java.io.ObjectOutputStream s)
+    private void writeObject(java.io.ObjectOutputStream s)
         throws IOException
     {
+        Entry<Object, Object> entryStack = null;
+
+        synchronized (this) {
         // Write out the length, threshold, loadfactor
         s.defaultWriteObject();
 
-        // Write out length, count of elements and then the key/value objects
+            // Write out length, count of elements
         s.writeInt(table.length);
         s.writeInt(count);
-        for (int index = table.length-1; index >= 0; index--) {
+
+            // Stack copies of the entries in the table
+            for (int index = 0; index < table.length; index++) {
             Entry entry = table[index];
 
             while (entry != null) {
-                s.writeObject(entry.key);
-                s.writeObject(entry.value);
+                    entryStack = new Entry<Object, Object>(0,
+                            entry.key, entry.value, entryStack);
                 entry = entry.next;
             }
         }
     }
+
+        // Write out the key/value objects from the stacked entries
+        while (entryStack != null) {
+            s.writeObject(entryStack.key);
+            s.writeObject(entryStack.value);
+            entryStack = entryStack.next;
+        }
+    }
 
     /**
      * Reconstitute the Hashtable from a stream (i.e., deserialize it).
      */
     private void readObject(java.io.ObjectInputStream s)