From 0cc9ddba0588953409a4398cdc22b1d7139a0c4f Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 14 Feb 2023 09:57:23 +0200 Subject: [PATCH] read and throw hwi error stream if stdout empty --- .../com/sparrowwallet/sparrow/io/Hwi.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Hwi.java b/src/main/java/com/sparrowwallet/sparrow/io/Hwi.java index 7f073c21..a9f7ff53 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Hwi.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Hwi.java @@ -290,9 +290,7 @@ public class Hwi { try { ProcessBuilder processBuilder = new ProcessBuilder(command); process = processBuilder.start(); - try(InputStreamReader reader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)) { - return CharStreams.toString(reader); - } + return getProcessOutput(process); } finally { deleteExtractionOnFailure(process, start); } @@ -318,14 +316,30 @@ public class Hwi { writer.flush(); } - try(InputStreamReader reader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)) { - return CharStreams.toString(reader); - } + return getProcessOutput(process); } finally { deleteExtractionOnFailure(process, start); } } + private String getProcessOutput(Process process) throws IOException { + String output; + try(InputStreamReader reader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)) { + output = CharStreams.toString(reader); + } + + if(output.isEmpty() && process.getErrorStream() != null) { + try(InputStreamReader reader = new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8)) { + String errorOutput = CharStreams.toString(reader); + if(!errorOutput.isEmpty()) { + throw new IOException(errorOutput); + } + } + } + + return output; + } + private synchronized File getHwiExecutable(Command command) { File hwiExecutable = Config.get().getHwi(); if(hwiExecutable != null && hwiExecutable.exists()) {