Skip to content

Commit b7ede41

Browse files
author
Brian Burkhalter
committed
8337716: ByteBuffer hashCode implementations are inconsistent
Reviewed-by: jpai, alanb Backport-of: 8bd3cd5
1 parent 2eb7709 commit b7ede41

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ package java.nio;
2929

3030
import java.lang.foreign.MemorySegment;
3131
import java.util.Objects;
32-
import jdk.internal.util.ArraysSupport;
3332

3433
/**
3534
#if[rw]
@@ -706,9 +705,6 @@ class Heap$Type$Buffer$RW$
706705
addr, segment)));
707706
}
708707

709-
public int hashCode() {
710-
return ArraysSupport.hashCode(hb, ix(position()), remaining(), 1);
711-
}
712708

713709
#end[byte]
714710

@@ -737,9 +733,6 @@ class Heap$Type$Buffer$RW$
737733
offset, segment);
738734
}
739735

740-
public int hashCode() {
741-
return ArraysSupport.hashCode(hb, ix(position()), remaining(), 1);
742-
}
743736
#end[char]
744737

745738

test/jdk/java/nio/Buffer/EqualsCompareTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
2525
import org.testng.annotations.DataProvider;
2626
import org.testng.annotations.Test;
2727

28+
import java.io.IOException;
2829
import java.lang.invoke.MethodHandle;
2930
import java.lang.invoke.MethodHandles;
3031
import java.lang.invoke.MethodType;
@@ -36,13 +37,21 @@
3637
import java.nio.FloatBuffer;
3738
import java.nio.IntBuffer;
3839
import java.nio.LongBuffer;
40+
import java.nio.MappedByteBuffer;
3941
import java.nio.ShortBuffer;
42+
import java.nio.channels.FileChannel;
43+
import java.nio.file.Files;
44+
import java.nio.file.Path;
4045
import java.util.HashMap;
4146
import java.util.Map;
47+
import java.util.Set;
4248
import java.util.function.BiFunction;
4349
import java.util.function.LongFunction;
4450
import java.util.stream.IntStream;
4551

52+
import static java.nio.charset.StandardCharsets.UTF_8;
53+
import static java.nio.file.StandardOpenOption.*;
54+
4655
/*
4756
* @test
4857
* @bug 8193085 8199773
@@ -713,4 +722,17 @@ static int[] ranges(int from, int to) {
713722
.distinct().toArray();
714723
}
715724
}
725+
726+
@Test
727+
void testHashCode() throws IOException {
728+
byte[] bytes = "hello world".getBytes(UTF_8);
729+
Path path = Files.createTempFile("", "");
730+
Files.write(path, bytes);
731+
try (FileChannel fc = FileChannel.open(path, READ, DELETE_ON_CLOSE)) {
732+
MappedByteBuffer one = fc.map(FileChannel.MapMode.READ_ONLY, 0, bytes.length);
733+
ByteBuffer two = ByteBuffer.wrap(bytes);
734+
Assert.assertEquals(one, two);
735+
Assert.assertEquals(one.hashCode(), two.hashCode());
736+
}
737+
}
716738
}

test/micro/org/openjdk/bench/java/nio/ByteBuffers.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -923,9 +923,4 @@ public double testDirectLoopGetDouble() {
923923
}
924924
return r;
925925
}
926-
927-
@Benchmark
928-
public int testHeapHashCode() {
929-
return heapByteBuffer.hashCode();
930-
}
931926
}

0 commit comments

Comments
 (0)