com.datastax.util.lang.ProcessUtil Maven / Gradle / Ivy
package com.datastax.util.lang;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class ProcessUtil {
private ProcessStreamHandler handler;
private String workingDir;
public ProcessUtil(){
}
public ProcessUtil(ProcessStreamHandler handler){
this.handler=handler;
}
public void setWorkingDir(String dir){
this.workingDir=dir;
}
// 保存进程的输入流信息
private List stdoutList = new ArrayList();
// 保存进程的错误流信息
private List erroroutList = new ArrayList();
public void execute(String command) {
// 先清空
stdoutList.clear();
erroroutList.clear();
Process p = null;
try {
p = Runtime.getRuntime().exec(command);
unblock(p);
} catch (IOException e) {
e.printStackTrace();
}
}
public void execute(String[] commands){
// 先清空
stdoutList.clear();
erroroutList.clear();
Process p = null;
try {
p = Runtime.getRuntime().exec(commands);
unblock(p);
} catch (IOException e) {
e.printStackTrace();
}
}
public void builder(String...commands){
// 先清空
stdoutList.clear();
erroroutList.clear();
Process p = null;
try {
if(null==workingDir) {
p = new ProcessBuilder(commands).start();
}else {
p = new ProcessBuilder(commands).directory(new File(workingDir)).start();
}
unblock(p);
} catch (IOException e) {
e.printStackTrace();
}
}
public void builder(List commands){
// 先清空
stdoutList.clear();
erroroutList.clear();
Process p = null;
try {
if(null==workingDir) {
p = new ProcessBuilder(commands).start();
}else {
p = new ProcessBuilder(commands).directory(new File(workingDir)).start();
}
unblock(p);
} catch (IOException e) {
e.printStackTrace();
}
}
public void unblock(Process p){
// 创建2个线程,分别读取输入流缓冲区和错误流缓冲区
ThreadUtil stdoutUtil = new ThreadUtil(p.getInputStream(), stdoutList,handler);
ThreadUtil erroroutUtil = new ThreadUtil(p.getErrorStream(), erroroutList,handler);
//启动线程读取缓冲区数据
stdoutUtil.start();
erroroutUtil.start();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public List getStdoutList() {
return stdoutList;
}
public List getErroroutList() {
return erroroutList;
}
public void executeBlock(String command) {
try {
Process p = Runtime.getRuntime().exec(command);
InputStream output=p.getInputStream();
handleStream(output);
InputStream error=p.getErrorStream();
handleStream(error);
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void handleStream(InputStream inputStream) throws IOException {
InputStreamReader isr = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(line);
}
}
class ThreadUtil implements Runnable {
// 设置读取的字符编码
private String character = "UTF-8";
private List list;
private InputStream inputStream;
private ProcessStreamHandler handler;
public ThreadUtil(InputStream inputStream, List list,ProcessStreamHandler handler) {
this.inputStream = inputStream;
this.list = list;
this.handler=handler;
}
public void start() {
Thread thread = new Thread(this);
thread.setDaemon(true);//将其设置为守护线程
thread.start();
}
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(inputStream, character));
String line = null;
while ((line = br.readLine()) != null) {
if (line != null) {
if(handler!=null){
handler.handle(line);
}else {
System.out.println(line);
}
list.add(line);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//释放资源
inputStream.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
ProcessUtil processUtil=new ProcessUtil();
//processUtil.setWorkingDir("/home/dataexa/Models/im2txt");
processUtil.setWorkingDir("/Users/andershong/Downloads/im2txt");
processUtil.builder("python3","run_inference.py","--dir_path","./picture");
for(String str : processUtil.getStdoutList()){
System.out.println(str);
}
}
}