< prev index next >

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

Print this page

        

@@ -228,10 +228,18 @@
         this(Math.max(2*t.size(), 11), 0.75f);
         putAll(t);
     }
 
     /**
+     * A constructor chained from {@link Properties} keeps Hashtable fields
+     * uninitialized since they are not used.
+     *
+     * @param dummy a dummy parameter
+     */
+    Hashtable(Void dummy) {}
+
+    /**
      * Returns the number of keys in this hashtable.
      *
      * @return  the number of keys in this hashtable.
      */
     public synchronized int size() {

@@ -547,22 +555,27 @@
      * This is a relatively expensive operation.
      *
      * @return  a clone of the hashtable
      */
     public synchronized Object clone() {
-        try {
-            Hashtable<?,?> t = (Hashtable<?,?>)super.clone();
+        Hashtable<?,?> t = cloneHashtable();
             t.table = new Entry<?,?>[table.length];
             for (int i = table.length ; i-- > 0 ; ) {
                 t.table[i] = (table[i] != null)
                     ? (Entry<?,?>) table[i].clone() : null;
             }
             t.keySet = null;
             t.entrySet = null;
             t.values = null;
             t.modCount = 0;
             return t;
+    }
+
+    /** Calls super.clone() */
+    final Hashtable<?,?> cloneHashtable() {
+        try {
+            return (Hashtable<?,?>)super.clone();
         } catch (CloneNotSupportedException e) {
             // this shouldn't happen, since we are Cloneable
             throw new InternalError(e);
         }
     }

@@ -1187,10 +1200,15 @@
      *             for each key-value mapping represented by the Hashtable
      *             The key-value mappings are emitted in no particular order.
      */
     private void writeObject(java.io.ObjectOutputStream s)
             throws IOException {
+        writeHashtable(s);
+    }
+ 
+    void writeHashtable(java.io.ObjectOutputStream s)
+            throws IOException {
         Entry<Object, Object> entryStack = null;
 
         synchronized (this) {
             // Write out the threshold and loadFactor
             s.defaultWriteObject();

@@ -1217,15 +1235,30 @@
             entryStack = entryStack.next;
         }
     }
 
     /**
+     * Write out the simulated threshold, loadfactor
+     * Called by Properties
+     */
+    final void defaultWriteHashtable(java.io.ObjectOutputStream s, int length,
+            float loadFactor) throws IOException {
+        this.threshold = (int)Math.min(length * loadFactor, MAX_ARRAY_SIZE + 1);
+        this.loadFactor = loadFactor;
+        s.defaultWriteObject();
+    }
+
+    /**
      * Reconstitute the Hashtable from a stream (i.e., deserialize it).
      */
     private void readObject(java.io.ObjectInputStream s)
-         throws IOException, ClassNotFoundException
-    {
+            throws IOException, ClassNotFoundException {
+        readHashtable(s);
+    }
+
+    void readHashtable(java.io.ObjectInputStream s)
+            throws IOException, ClassNotFoundException {
         // Read in the threshold and loadFactor
         s.defaultReadObject();
 
         // Validate loadFactor (ignore threshold - it will be re-computed)
         if (loadFactor <= 0 || Float.isNaN(loadFactor))
< prev index next >