io.github.awidesky.processExecutor.ProcessIO Maven / Gradle / Ivy
package io.github.awidesky.processExecutor;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.Objects;
import java.util.Scanner;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;
import io.github.awidesky.guiUtil.Logger;
import io.github.awidesky.guiUtil.SwingDialogs;
public class ProcessIO {
private static final Charset NATIVECHARSET = Charset.forName(System.getProperty("native.encoding"));
private Consumer stdout = null;
private Consumer stderr = null;
private Consumer stdin = null;
public ProcessIO() {}
public ProcessIO(Logger logger) {
setStdout(logger);
setStderr(logger);
}
public ProcessIO(Logger out, Logger err) {
setStdout(out);
setStderr(err);
}
public ProcessIO(Logger logger, Consumer stdin) {
this(logger);
setBufferedStdin(stdin);
}
public ProcessIO(Consumer stdout, Consumer stderr) {
setBufferedStdout(stdout);
setBufferedStderr(stderr);
}
public ProcessIO(Consumer stdout, Consumer stderr, Consumer stdin) {
this(stdout, stderr);
setBufferedStdin(stdin);
}
public ProcessIO stdout(InputStream is) {
stdout.accept(is);
try {
is.close();
} catch (IOException e) {
// TODO describe default error handling behavior
e.printStackTrace();
}
return this;
}
public ProcessIO stderr(InputStream is) {
stderr.accept(is);
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this;
}
public ProcessIO stdin(OutputStream os) {
if(stdin != null) stdin.accept(os);
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this;
}
public ProcessIO setStdout(Consumer stdout) {
this.stdout = stdout;
return this;
}
public ProcessIO setStderr(Consumer stderr) {
this.stderr = stderr;
return this;
}
public ProcessIO setStdin(Consumer stdin) {
this.stdin = stdin;
return this;
}
/**
* If an Exception is throw, shows the error via swingDialogs.
*/
public ProcessIO setBufferedStdout(Consumer stdout) {
this.stdout = is -> {
try (BufferedReader br = new BufferedReader(new InputStreamReader(is, NATIVECHARSET))) {
stdout.accept(br);
} catch (IOException e) {
SwingDialogs.error("Unable to close process input stream!", "%e%", e, false);
}
};
return this;
}
/**
* If an Exception is throw, shows the error via swingDialogs.
*/
public ProcessIO setBufferedStderr(Consumer stderr) {
this.stderr = is -> {
try (BufferedReader br = new BufferedReader(new InputStreamReader(is, NATIVECHARSET))) {
stderr.accept(br);
} catch (IOException e) {
SwingDialogs.error("Unable to close process input stream!", "%e%", e, false);
}
};
return this;
}
/**
* If an Exception is throw, shows the error via swingDialogs.
*/
public ProcessIO setBufferedStdin(Consumer stdin) {
this.stdin = is -> {
try (BufferedWriter br = new BufferedWriter(new OutputStreamWriter(is, NATIVECHARSET))) {
stdin.accept(br);
} catch (IOException e) {
SwingDialogs.error("Unable to close process input stream!", "%e%", e, false); //TODO : change to printstacktrace to all
}
};
return this;
}
/**
* Does not show or throw any error.
*/
public ProcessIO setPrintStdout(Consumer stdout) {
this.stdout = is -> {
stdout.accept(new Scanner(new InputStreamReader(is, NATIVECHARSET)));
};
return this;
}
/**
* Does not show or throw any error.
*/
public ProcessIO setPrintStderr(Consumer stderr) {
this.stderr = is -> {
stderr.accept(new Scanner(new InputStreamReader(is, NATIVECHARSET)));
};
return this;
}
/**
* Does not show or throw any error.
*/
public ProcessIO setPrintStdin(Consumer stdin) {
this.stdin = is -> {
stdin.accept(new PrintWriter(new OutputStreamWriter(is, NATIVECHARSET), true));
};
return this;
}
public ProcessIO setStdout(Logger logger) {
setBufferedStdout(br -> br.lines().forEach(logger::info));
return this;
}
public ProcessIO setStderr(Logger logger) {
setBufferedStderr(br -> br.lines().forEach(logger::error));
return this;
}
/**
* input stream will terminate when null is supplied.
*/
public ProcessIO setStdin(Supplier s) {
setPrintStdin(pw -> {
Stream.generate(s::get).takeWhile(Objects::nonNull).forEach(pw::println);
});
return this;
}
public ProcessIO setStdin(Stream s) {
setPrintStdin(pw -> s.forEach(pw::println));
return this;
}
public static Charset getNativeChearset() {
return NATIVECHARSET;
}
}