2011-05-20 54 views
2

我有下面的代碼,我想不通爲什麼它不會工作:Java字節數組輸出流給出什麼

final ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
final String p1 = "HELLO WORLD"; 
process(p1, bos); 
Assert.assertEquals("BOS value should be: "+p1, p1, bos.toString("UTF-8")); 

它打印:

junit.framework.ComparisonFailure :BOS值應爲:HELLO WORLD預期:< [HELLO WORLD]>但:< []> 在junit.framework.Assert.assertEquals(Assert.java:81)等...

和過程是這樣的:

public static void process(final String p1, final OutputStream os) { 
    final Runtime rt = Runtime.getRuntime(); 
    try { 
     final String command = "echo " + p1; 
     log.info("Executing Command: " + command); 
     final Process proc = rt.exec(command); 

     // gobble error and output 
     StreamGobbler.go(proc.getErrorStream(), null); 
     StreamGobbler.go(proc.getInputStream(), os); 

     // wait for the exit 
     try { 
      final int exitVal = proc.waitFor(); 
      log.info("Command Exit Code: " + exitVal); 
     } catch (InterruptedException e) { 
      log.error("Interrupted while waiting for command to execute", e); 
     } 
    } catch (IOException e) { 
     log.error("IO Exception while executing command", e); 
    } 
} 

private static class StreamGobbler extends Thread { 
    private final InputStream is; 
    private final OutputStream os; 

    private static StreamGobbler go(InputStream is, OutputStream os) { 
     final StreamGobbler gob = new StreamGobbler(is, os); 
     gob.start(); 
     return gob; 
    } 

    private StreamGobbler(InputStream is, OutputStream os) { 
     this.is = is; 
     this.os = os; 
    } 

    public void run() { 
     try { 
      final PrintWriter pw = ((os == null) ? null : new PrintWriter(os)); 
      final InputStreamReader isr = new InputStreamReader(is); 
      final BufferedReader br = new BufferedReader(isr); 
      String line = null; 
      while ((line = br.readLine()) != null) { 
       if (pw != null) { 
        pw.println(line); 
       } 
       log.info(line); // Prints HELLO WORLD to log 
      } 
      if (pw != null) { 
       pw.flush(); 
      } 
     } catch (IOException ioe) { 
      log.error("IO error while globbing", ioe); 
     } 
    } 

當我運行JUnit測試我得到一個空字符串作爲實際。我不明白爲什麼這不會工作。

編輯:我使用RHEL5和日食3.6,如果這有所作爲。

+0

提到您的操作系統;答案可能取決於echo是應用程序還是shell函數。 – McDowell 2011-05-20 23:33:51

+0

RHEL5。我在StreamGlobber的循環內放入了一個log.info(行),並打印出HELLO WORLD。但我的ByteArrayOutputStream沒有。 – 2011-05-20 23:35:43

+0

你的「命令退出代碼」和「執行命令」不會顯示在你的輸出中 - 你是否修剪掉了? – 2011-05-20 23:39:30

回答

4

也許你應該等待的線程上填充流:

Thread thr = StreamGobbler.go(proc.getInputStream(), os); 

    // wait for the exit 
    try { 
     final int exitVal = proc.waitFor(); 
     log.info("Command Exit Code: " + exitVal); 
     thr.join();//waits for the gobbler that processes the stdout of the process 
    } catch (InterruptedException e) { 
     log.error("Interrupted while waiting for command to execute", e); 
    } 
+0

工作正常。我勒個去?爲什麼我必須這樣做? – 2011-05-20 23:45:24

+2

導致主線程可能會完成方法(並通過assertEquals),然後gobbler線程有機會運行並收集任何數據 – 2011-05-20 23:47:27