8337716: ByteBuffer hashCode implementations are inconsistent

Reviewed-by: alanb, liach
This commit is contained in:
Brian Burkhalter 2024-08-04 15:42:51 +00:00
parent 367e0a6556
commit 8bd3cd5156
3 changed files with 24 additions and 14 deletions

View File

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

View File

@ -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());
}
}
}

View File

@ -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();
}
}