/** {@inheritDoc} */ @Override public void writeLine(String line) throws IOException { lineConsumer.writeLine(line + System.lineSeparator()); }
@Override public void run() { String line; try { while ((line = bufferedReader.readLine()) != null) { lineConsumer.writeLine(line); } } catch (IOException e) { exception = e; } finally { synchronized (this) { done = true; notifyAll(); } } } }
/** * Writes stdout of the process to consumer.<br> * Supposes that stderr of the process is redirected to stdout. * * @param p process to read output from * @param stdout a consumer where stdout will be redirected * @throws IOException */ public static void process(Process p, LineConsumer stdout) throws IOException { try (BufferedReader inputReader = new BufferedReader(new InputStreamReader(p.getInputStream()))) { String line; while ((line = inputReader.readLine()) != null) { stdout.writeLine(line); } } }
@Override public void writeLine(String line) throws IOException { if (errorIndicator.isError(line)) { lineConsumer.writeLine(line); } }
/** * Writes stdout and stderr of the process to consumers.<br> * Supposes that stderr of the process is redirected to stdout. * * @param p process to read output from * @param stdout a consumer where stdout will be redirected * @param stderr a consumer where stderr will be redirected * @throws IOException */ public static void process(Process p, LineConsumer stdout, LineConsumer stderr) throws IOException { try (BufferedReader inputReader = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream()))) { String line; while ((line = inputReader.readLine()) != null) { stdout.writeLine(line); } while ((line = errorReader.readLine()) != null) { stderr.writeLine(line); } } }
for (LineConsumer lineConsumer : lineConsumers) { try { lineConsumer.writeLine(line); } catch (ClosedByInterruptException interrupted) { Thread.currentThread().interrupt();
for (LineConsumer lineConsumer : lineConsumers) { try { lineConsumer.writeLine(line); } catch (ConsumerAlreadyClosedException | ClosedByInterruptException e) { lineConsumers.remove(