Skip to content

Instantly share code, notes, and snippets.

@volgar1x
Created October 10, 2023 18:53
Show Gist options
  • Save volgar1x/3b27360eac7b989c167bf7d548135f9c to your computer and use it in GitHub Desktop.
Save volgar1x/3b27360eac7b989c167bf7d548135f9c to your computer and use it in GitHub Desktop.
GraalVM CE 21: java.nio.file.InvalidPathException
FROM ghcr.io/graalvm/graalvm-community:21 AS build
WORKDIR /app
COPY NioPath.java .
RUN javac NioPath.java && native-image -Dsun.jnu.encoding=UTF-8 NioPath
FROM debian:12-slim AS production
COPY --from=build /app/niopath /bin/niopath
CMD ["/bin/niopath", "àéïõû.txt"]
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.Charset;
import java.nio.ByteBuffer;
import java.io.File;
public class NioPath {
public static void main(String[] args) {
test("UTF-8", args[0]);
test(System.getProperty("file.encoding"), args[0]);
test(System.getProperty("sun.jnu.encoding"), args[0]);
test(System.getProperty("sun.jnu.encoding"), "àéïõûy.txt");
// throws java.nio.file.InvalidPathException
try { System.out.println(Paths.get(args[0])); }
catch (Throwable th) { System.out.println(th); }
// ok
try { System.out.println(new File(args[0])); }
catch (Throwable th) { System.out.println(th); }
// throws java.nio.file.InvalidPathException
try { System.out.println(Paths.get(new File(args[0]).toURI())); }
catch (Throwable th) { System.out.println(th); }
// throws java.nio.file.InvalidPathException
try { System.out.println(new File(args[0]).toPath()); }
catch (Throwable th) { System.out.println(th); }
}
private static void test(String charsetName, String value) {
System.out.println("testing with charset: " + charsetName);
Charset charset = Charset.forName(charsetName);
ByteBuffer encoded = charset.encode(value);
System.out.print("encoded: ");
while (encoded.hasRemaining()) {
System.out.print(Integer.toHexString(encoded.get() & 0xff));
}
System.out.print("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment