8270416: Enhance construction of Identity maps

Reviewed-by: dfuchs, chegar, rhalade, ahgross, smarks, robm
This commit is contained in:
Julia Boes 2021-08-25 11:41:26 +00:00 committed by Henry Jen
parent 6b6f829b46
commit 5832a34404
3 changed files with 41 additions and 22 deletions

View file

@ -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