简体   繁体   English

InputStream和StreamGobbler有什么区别?

[英]What's the difference between InputStream and StreamGobbler?

In the code below, the author uses Runtime to call a sub-process. 在下面的代码中,作者使用Runtime调用子流程。 But I don't understand why he uses StreamGobbler . 但是我不明白他为什么使用StreamGobbler What will happen if replace it by InputStream ? 如果用InputStream替换它将发生什么? Please help me, thanks! 请帮助我,谢谢!

public class GoodWindowsExec   
{   
    public static void main(String args[])   
    {   

        try   
        {               
            String osName = System.getProperty("os.name" );   
            System.out.println("osName: " + osName);   
            String[] cmd = new String[3];   

            if(osName.equals("Windows XP") ||osName.equals("Windows 2000"))   
            {   
                cmd[0] = "cmd.exe" ;   
                cmd[1] = "/C" ;   
                cmd[2] = args[0];   
            }   
            else if( osName.equals( "Windows 98" ) )   
            {   
                cmd[0] = "command.com" ;   
                cmd[1] = "/C" ;   
                cmd[2] = args[0];   
            }   

            Runtime rt = Runtime.getRuntime();   
            System.out.println("Execing " + cmd[0] + " " + cmd[1]+ " " + cmd[2]);   
            Process proc = rt.exec(cmd);   
            // any error message?   
            StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");  //Can I replace StreamGobbler by InputStream?        
            // any output?   
            StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");   //As above

            // kick them off   
            errorGobbler.start();   
            outputGobbler.start();   

            // any error???   
            int exitVal = proc.waitFor();   
            System.out.println("ExitValue: " + exitVal);   

        } catch (Throwable t){   
            t.printStackTrace();   
        }   
    }   
}

BufferedReader brNormal = new BufferedReader(new InputStreamReader(process.getInputStream()));
String s = null;
while ((s = brNormal.readLine()) != null) {
    logger.info("RawToRgbConverter.exe", s);
}
brNormal.close();
BufferedReader brError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((s = brError.readLine()) != null) {
    logger.error("RawToRgbConverter.exe", s);
}
brError.close();

Even as I don't know the StreamGobbler class, this is obviously a threaded implementation to copy the output and error streams to a given target. 即使我不知道StreamGobbler类,这显然也是将输出和错误流复制到给定目标的线程实现。 Thus, no you cannot simply replace it, as the multi-threading is indeed necessary. 因此,没有,您不能简单地替换它,因为确实需要多线程。 A simple input stream would just sit there, but not actually do anything. 一个简单的输入流只会坐在那里,但实际上什么也没做。

Note however, that this complete solution is outdated since Java 1.5 which introduced the ProcessBuilder and the automatic redirects. 但是请注意,自Java 1.5引入了ProcessBuilder和自动重定向以来,这种完整的解决方案已经过时了。 See the javadoc at https://docs.oracle.com/javase/8/docs/api/ 请参阅https://docs.oracle.com/javase/8/docs/api/中的javadoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM