From 8bd3cd51562ff9e76fa0e3d49d38e6e19210f878 Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Sun, 4 Aug 2024 15:42:51 +0000 Subject: [PATCH] 8337716: ByteBuffer hashCode implementations are inconsistent Reviewed-by: alanb, liach --- .../java/nio/Heap-X-Buffer.java.template | 7 ------ .../java/nio/Buffer/EqualsCompareTest.java | 24 ++++++++++++++++++- .../openjdk/bench/java/nio/ByteBuffers.java | 7 +----- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template b/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template index 8fbc1a3b69d..4820725d6c4 100644 --- a/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template +++ b/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template @@ -29,7 +29,6 @@ package java.nio; import java.lang.foreign.MemorySegment; import java.util.Objects; -import jdk.internal.util.ArraysSupport; /** #if[rw] @@ -706,9 +705,6 @@ class Heap$Type$Buffer$RW$ addr, segment))); } - public int hashCode() { - return ArraysSupport.hashCode(hb, ix(position()), remaining(), 1); - } #end[byte] @@ -737,9 +733,6 @@ class Heap$Type$Buffer$RW$ offset, segment); } - public int hashCode() { - return ArraysSupport.hashCode(hb, ix(position()), remaining(), 1); - } #end[char] diff --git a/test/jdk/java/nio/Buffer/EqualsCompareTest.java b/test/jdk/java/nio/Buffer/EqualsCompareTest.java index baee6412084..03bd7c26a58 100644 --- a/test/jdk/java/nio/Buffer/EqualsCompareTest.java +++ b/test/jdk/java/nio/Buffer/EqualsCompareTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2024, 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,7 @@ import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; +import java.io.IOException; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; @@ -36,13 +37,21 @@ import java.nio.DoubleBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.nio.LongBuffer; +import java.nio.MappedByteBuffer; import java.nio.ShortBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.function.BiFunction; import java.util.function.LongFunction; import java.util.stream.IntStream; +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.nio.file.StandardOpenOption.*; + /* * @test * @bug 8193085 8199773 @@ -713,4 +722,17 @@ public class EqualsCompareTest { .distinct().toArray(); } } + + @Test + void testHashCode() throws IOException { + byte[] bytes = "hello world".getBytes(UTF_8); + Path path = Files.createTempFile("", ""); + Files.write(path, bytes); + try (FileChannel fc = FileChannel.open(path, READ, DELETE_ON_CLOSE)) { + MappedByteBuffer one = fc.map(FileChannel.MapMode.READ_ONLY, 0, bytes.length); + ByteBuffer two = ByteBuffer.wrap(bytes); + Assert.assertEquals(one, two); + Assert.assertEquals(one.hashCode(), two.hashCode()); + } + } } diff --git a/test/micro/org/openjdk/bench/java/nio/ByteBuffers.java b/test/micro/org/openjdk/bench/java/nio/ByteBuffers.java index c2f4e93313c..48f75a3ec81 100644 --- a/test/micro/org/openjdk/bench/java/nio/ByteBuffers.java +++ b/test/micro/org/openjdk/bench/java/nio/ByteBuffers.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2022, 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 @@ -923,9 +923,4 @@ public class ByteBuffers { } return r; } - - @Benchmark - public int testHeapHashCode() { - return heapByteBuffer.hashCode(); - } }