aboutsummaryrefslogtreecommitdiff
path: root/src/java.base/share/classes/java/util/IdentityHashMap.java
diff options
context:
space:
mode:
authorEvan Whelan <ewhelan@openjdk.org>2021-09-15 14:30:40 +0000
committerJulia Boes <jboes@openjdk.org>2021-09-15 14:30:40 +0000
commit94f41162f4cb14cd48860e002a1c97e50f42a556 (patch)
treeced40e82ff7694cafee44637ba274e2fd0ed8820 /src/java.base/share/classes/java/util/IdentityHashMap.java
parentf7f9a73a1e73576fd276fdc2a6305c136a2dde52 (diff)
downloadopenjdk-94f41162f4cb14cd48860e002a1c97e50f42a556.tar.gz
openjdk-94f41162f4cb14cd48860e002a1c97e50f42a556.zip
8270416: Enhance construction of Identity maps
Reviewed-by: jboes Backport-of: d445bf3f60c654bee77dcabe86f6fa28eaf5abfa
Diffstat (limited to 'src/java.base/share/classes/java/util/IdentityHashMap.java')
-rw-r--r--src/java.base/share/classes/java/util/IdentityHashMap.java22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/java.base/share/classes/java/util/IdentityHashMap.java b/src/java.base/share/classes/java/util/IdentityHashMap.java
index 34d6722ac09..4795c30b3d5 100644
--- a/src/java.base/share/classes/java/util/IdentityHashMap.java
+++ b/src/java.base/share/classes/java/util/IdentityHashMap.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,8 @@
package java.util;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
@@ -1267,12 +1269,12 @@ public class IdentityHashMap<K,V>
* particular order.
*/
@java.io.Serial
- private void writeObject(java.io.ObjectOutputStream s)
+ private void writeObject(ObjectOutputStream s)
throws java.io.IOException {
- // Write out and any hidden stuff
+ // Write out size (number of mappings) and any hidden stuff
s.defaultWriteObject();
- // Write out size (number of Mappings)
+ // Write out size again (maintained for backward compatibility)
s.writeInt(size);
// Write out keys and values (alternating)
@@ -1291,18 +1293,20 @@ public class IdentityHashMap<K,V>
* deserializes it).
*/
@java.io.Serial
- private void readObject(java.io.ObjectInputStream s)
+ private void readObject(ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
- // Read in any hidden stuff
- s.defaultReadObject();
+ // Size (number of mappings) is written to the stream twice
+ // Read first size value and ignore it
+ s.readFields();
- // Read in size (number of Mappings)
+ // Read second size value, validate and assign to size field
int size = s.readInt();
if (size < 0)
throw new java.io.StreamCorruptedException
("Illegal mappings count: " + size);
int cap = capacity(size);
- SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, cap);
+ SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, cap*2);
+ this.size = size;
init(cap);
// Read the keys and values, and put the mappings in the table