Skip to content

Commit

Permalink
PDFBOX-5913: don't spawn a cmd / command subprocess to get windir, as…
Browse files Browse the repository at this point in the history
… suggested by Jakob Heher

git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1922318 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
THausherr committed Dec 5, 2024
1 parent 605ce3c commit 624aef9
Showing 1 changed file with 2 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}
*
Expand All @@ -86,9 +56,9 @@ public List<File> 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
Expand Down

0 comments on commit 624aef9

Please sign in to comment.