diff --git a/core/src/main/java/com/graphhopper/storage/MMapDataAccess.java b/core/src/main/java/com/graphhopper/storage/MMapDataAccess.java index 330b3b95473..b9bf24276c1 100644 --- a/core/src/main/java/com/graphhopper/storage/MMapDataAccess.java +++ b/core/src/main/java/com/graphhopper/storage/MMapDataAccess.java @@ -65,7 +65,7 @@ public final class MMapDataAccess extends AbstractDataAccess { this.allowWrites = allowWrites; } - private static MethodHandle getByteBufferCleaner() { + static MethodHandle getByteBufferCleaner() { try { Class unsafeClass = Class.forName("sun.misc.Unsafe"); Lookup lookup = MethodHandles.privateLookupIn(unsafeClass, MethodHandles.lookup()); diff --git a/core/src/test/java/com/graphhopper/storage/MMapDataAccessTest.java b/core/src/test/java/com/graphhopper/storage/MMapDataAccessTest.java index 1082e3d9a98..fa8876270c1 100644 --- a/core/src/test/java/com/graphhopper/storage/MMapDataAccessTest.java +++ b/core/src/test/java/com/graphhopper/storage/MMapDataAccessTest.java @@ -19,6 +19,9 @@ import org.junit.jupiter.api.Test; +import java.lang.invoke.MethodHandle; +import java.nio.ByteBuffer; + import static org.junit.jupiter.api.Assertions.*; /** @@ -59,4 +62,17 @@ public void textMixMMAP2RAM() { assertEquals(123, da.getInt(7 * 4)); da.close(); } + + @Test + public void testByteBufferCleaner() { + MethodHandle bufferCleaner = MMapDataAccess.getByteBufferCleaner(); + assertNotNull(bufferCleaner); + try { + bufferCleaner.invokeExact(ByteBuffer.allocate(10)); + fail("Cleaner invocation should fail for a non-direct buffer"); + } catch (Throwable t) { + assertInstanceOf(IllegalArgumentException.class, t); + assertEquals("buffer is non-direct", t.getMessage()); + } + } }