package com.persagy.cameractl.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/*
 * 新线程里读取cmd命令的输入流和错误流以免造成进程堵塞
 * */
public class CmdStreamThread {
	private BufferedReader inputBfr = null;
	private BufferedReader errBfr = null;
	public String outString;
	public String errString;

	public CmdStreamThread(InputStream inputStream,InputStream errStream) {
		this.inputBfr = new BufferedReader(new InputStreamReader(inputStream));
		this.errBfr = new BufferedReader(new InputStreamReader(errStream));
		new Thread(new InputStreamThread()).start();
		new Thread(new ErrStreamThread()).start();
	}
	
	/*
	 * 读取输入流
	 * */
	class InputStreamThread implements Runnable {
		@Override
		public void run() {
			try {
				String s;
				StringBuilder sb = new StringBuilder();
				while ((s = inputBfr.readLine()) != null) {
					sb.append(s);
				}
				inputBfr.close();
				outString = sb.toString();
			} catch (Exception e) {
				outString = e.getMessage();
			}
		}
	};
	
	/*
	 * 读取错误流
	 * */
	class ErrStreamThread implements Runnable {
		@Override
		public void run() {
			try {
				String s;
				StringBuilder sb = new StringBuilder();
				while ((s = errBfr.readLine()) != null) {
					sb.append(s);
				}
				errBfr.close();
				errString = sb.toString();
			} catch (Exception e) {
				errString = e.getMessage();
			}
		}
	};

}