/** {@inheritDoc} */ @Override public void close() throws IOException { lineConsumer.close(); } }
/** {@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(); } } } }
@Override public void close() throws IOException { lineConsumer.close(); }
/** * 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); } } }
/** Closes all unclosed subconsumers. */ @Override public void close() { if (isOpen) { isOpen = false; for (LineConsumer lineConsumer : lineConsumers) { try { lineConsumer.close(); } catch (IOException e) { LOG.error( String.format("An error occurred while closing the line consumer %s", lineConsumer), e); } } } }
@Override public void writeLine(String line) throws IOException { if (errorIndicator.isError(line)) { lineConsumer.writeLine(line); } }
/** Closes all unclosed subconsumers. */ @Override public void close() { if (isOpen) { lock.writeLock().lock(); try { isOpen = false; for (LineConsumer lineConsumer : lineConsumers) { try { lineConsumer.close(); } catch (IOException e) { LOG.error( String.format("An error occurred while closing the line consumer %s", lineConsumer), e); } } } finally { lock.writeLock().unlock(); } } }
/** * 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(