package kalang.script;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;

/**
 *
 * @author Kason Yang
 */
public class Proc {

    private final Process process;

    public Proc(String[] command, @Nullable String workingDir) throws IOException {
        ProcessBuilder pb = new ProcessBuilder(command);
        if (workingDir != null) {
            pb.directory(new File(workingDir));
        }
        process = pb.start();
    }

    public OutputStream getOutputStream() {
        return process.getOutputStream();
    }

    public InputStream getInputStream() {
        return process.getInputStream();
    }

    public InputStream getErrorStream() {
        return process.getErrorStream();
    }

    public int waitFor() throws InterruptedException {
        return process.waitFor();
    }

    public boolean waitFor(long timeout, TimeUnit unit) throws InterruptedException {
        return process.waitFor(timeout, unit);
    }

    public int exitValue() {
        return process.exitValue();
    }

    public void destroy() {
        process.destroy();
    }

    public Process destroyForcibly() {
        return process.destroyForcibly();
    }

    public boolean isAlive() {
        return process.isAlive();
    }

    public String out() throws IOException {
        return inputStreamToString(process.getInputStream(), Charset.defaultCharset().name());
    }

    public String err() throws IOException {
        return inputStreamToString(process.getErrorStream(), Charset.defaultCharset().name());
    }

    private String inputStreamToString(InputStream is, String charset) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int rlen;
        while ((rlen = is.read(buffer)) > 0) {
            bos.write(buffer, 0, rlen);
        }
        return bos.toString(charset);
    }

}

@@ -1126,32 +1126,170 @@
 ut, unit);%0A    %7D
+%0A    %0A    public boolean waitFor(long timeout) throws InterruptedException %7B%0A        return waitFor(timeout, TimeUnit.MILLISECONDS);%0A    %7D
 %0A%0A    public int

add method:waitFor(long timeout)