All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.buildobjects.process.IoHandler Maven / Gradle / Ivy

package org.buildobjects.process;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

class IoHandler {
    private final OutputConsumptionThread stdout;
    private final OutputConsumptionThread stderr;
    Thread inFeeder;


    IoHandler(InputStream stdin, OutputConsumptionThread stdout, OutputConsumptionThread stderr, Process process) {
        this.stdout = stdout;
        this.stderr = stderr;
        InputStream out = process.getInputStream();
        InputStream err = process.getErrorStream();
        OutputStream in = process.getOutputStream();

        stdout.startConsumption(out);
        stderr.startConsumption(err);
        inFeeder = startConsumption(in, stdin, true);
    }

    List joinConsumption() throws InterruptedException {
        inFeeder.join();
        stdout.join();
        stderr.join();

        List exceptions = new ArrayList();

        if (stdout.getThrowable() != null) {
            exceptions.add(stdout.getThrowable());
        }

        if (stderr.getThrowable() != null) {
            exceptions.add(stderr.getThrowable());
        }

        return exceptions;

    }

    void cancelConsumption() {
        inFeeder.interrupt();
        stdout.interrupt();
        stderr.interrupt();
    }

    Thread startConsumption(OutputStream stdout, InputStream out, boolean closeAfterWriting) {
        Thread consumer;
        consumer = new Thread(new StreamCopyRunner(out, stdout, closeAfterWriting));
        consumer.start();
        return consumer;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy