Changeset 13473 in josm for trunk/src


Ignore:
Timestamp:
2018-02-27T19:44:48+01:00 (7 years ago)
Author:
Don-vip
Message:

fix #15992 - more robust implementation of Utils.execOutput. Ensure calling a process never blocks the JVM

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r13472 r13473  
    1010import java.awt.font.FontRenderContext;
    1111import java.awt.font.GlyphVector;
    12 import java.io.BufferedReader;
    1312import java.io.ByteArrayOutputStream;
    1413import java.io.Closeable;
     
    1716import java.io.IOException;
    1817import java.io.InputStream;
    19 import java.io.InputStreamReader;
    2018import java.io.UnsupportedEncodingException;
    2119import java.lang.reflect.AccessibleObject;
     
    856854            Logging.debug(join(" ", command));
    857855        }
    858         Process p = new ProcessBuilder(command).redirectErrorStream(true).start();
    859         try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8))) {
    860             StringBuilder all = null;
    861             String line;
    862             while ((line = input.readLine()) != null) {
    863                 if (all == null) {
    864                     all = new StringBuilder(line);
    865                 } else {
    866                     all.append('\n')
    867                        .append(line);
    868                 }
    869             }
    870             String msg = all != null ? all.toString() : null;
    871             if (!p.waitFor(timeout, unit) || p.exitValue() != 0) {
    872                 throw new ExecutionException(msg, null);
    873             }
    874             return msg;
    875         }
     856        Path out = Files.createTempFile("josm_exec_", ".txt");
     857        Process p = new ProcessBuilder(command).redirectErrorStream(true).redirectOutput(out.toFile()).start();
     858        if (!p.waitFor(timeout, unit) || p.exitValue() != 0) {
     859            throw new ExecutionException(command.toString(), null);
     860        }
     861        String msg = String.join("\n", Files.readAllLines(out)).trim();
     862        try {
     863            Files.delete(out);
     864        } catch (IOException e) {
     865            Logging.warn(e);
     866        }
     867        return msg;
    876868    }
    877869
Note: See TracChangeset for help on using the changeset viewer.