From 624aef9c2610facbcd5f0b0f8a65b36c4ef9996e Mon Sep 17 00:00:00 2001 From: Tilman Hausherr Date: Thu, 5 Dec 2024 10:44:34 +0000 Subject: [PATCH] PDFBOX-5913: don't spawn a cmd / command subprocess to get windir, as suggested by Jakob Heher git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1922318 13f79535-47bb-0310-9956-ffa450edef68 --- .../util/autodetect/WindowsFontDirFinder.java | 34 ++----------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/fontbox/src/main/java/org/apache/fontbox/util/autodetect/WindowsFontDirFinder.java b/fontbox/src/main/java/org/apache/fontbox/util/autodetect/WindowsFontDirFinder.java index 811235a1912..66910c85611 100644 --- a/fontbox/src/main/java/org/apache/fontbox/util/autodetect/WindowsFontDirFinder.java +++ b/fontbox/src/main/java/org/apache/fontbox/util/autodetect/WindowsFontDirFinder.java @@ -17,11 +17,7 @@ package org.apache.fontbox.util.autodetect; -import java.io.BufferedReader; import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; import java.util.List; import org.apache.logging.log4j.Logger; @@ -36,32 +32,6 @@ public class WindowsFontDirFinder implements FontDirFinder private static final Logger LOG = LogManager.getLogger(WindowsFontDirFinder.class); - /** - * Attempts to read windir environment variable on windows (disclaimer: This is a bit dirty but seems to work - * nicely). - */ - private String getWinDir(String osName) throws IOException - { - Process process; - Runtime runtime = Runtime.getRuntime(); - String cmd; - if (osName.startsWith("Windows 9")) - { - cmd = "command.com"; - } - else - { - cmd = "cmd.exe"; - } - String[] cmdArray = { cmd, "/c", "echo", "%windir%" }; - process = runtime.exec(cmdArray); - try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( - process.getInputStream(), StandardCharsets.ISO_8859_1))) - { - return bufferedReader.readLine(); - } - } - /** * {@inheritDoc} * @@ -86,9 +56,9 @@ public List find() { try { - windir = getWinDir(osName); + windir = System.getenv("windir"); } - catch (IOException | SecurityException e) + catch (SecurityException e) { LOG.debug("Couldn't get Windows font directories - ignoring", e); // should continue if this fails