diff --git a/test/jdk/java/foreign/StdLibTest.java b/test/jdk/java/foreign/StdLibTest.java index 07b20786ee7..a8f9e2217c8 100644 --- a/test/jdk/java/foreign/StdLibTest.java +++ b/test/jdk/java/foreign/StdLibTest.java @@ -62,6 +62,14 @@ public class StdLibTest extends NativeTestHelper { final static Charset nativeCharset = Charset.forName(System.getProperty("native.encoding")); + static MemorySegment allocateSegmentForString(Arena arena, String str) { + try { + return arena.allocateFrom(str, nativeCharset); + } catch (IllegalArgumentException ex) { + return arena.allocateFrom(str); + } + } + private StdLibHelper stdLibHelper = new StdLibHelper(); @Test(dataProvider = "stringPairs") @@ -315,7 +323,7 @@ int rand() throws Throwable { int printf(String format, List args) throws Throwable { try (var arena = Arena.ofConfined()) { - MemorySegment formatStr = arena.allocateFrom(format, nativeCharset); + MemorySegment formatStr = allocateSegmentFromString(arena, format); return (int)specializedPrintf(args).invokeExact(formatStr, args.stream().map(a -> a.nativeValue(arena)).toArray()); } @@ -396,7 +404,7 @@ enum PrintfArg { INT(int.class, C_INT, "%d", "%d", arena -> 42, 42), LONG(long.class, C_LONG_LONG, "%lld", "%d", arena -> 84L, 84L), DOUBLE(double.class, C_DOUBLE, "%.4f", "%.4f", arena -> 1.2345d, 1.2345d), - STRING(MemorySegment.class, C_POINTER, "%s", "%s", arena -> arena.allocateFrom("str", nativeCharset), "str"); + STRING(MemorySegment.class, C_POINTER, "%s", "%s", arena -> allocateSegmentFromString(arena, "str"), "str"); final Class carrier; final ValueLayout layout;