无法从groovy执行中捕获完整输出 [英] Unable to capture full output from groovy execute

查看:664
本文介绍了无法从groovy执行中捕获完整输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试列出包含大约1200个项目的svn文件夹的内容。沿着执行外部在一个groovy脚本中的程序并捕获输出,我开发了以下代码

I am trying to list the content fo an svn folder which contains about 1200 items. Along the lines in "Execute a external program within a groovy script and capture the output", I developed the following code

def svnCommand = "svn list ${repoUrl}"
def sout = new StringBuilder()
def serr = new StringBuilder()

Process sproc = svnCommand.execute()
sproc.consumeProcessOutput(sout, serr)
sproc.waitForProcessOutput()
println sout

如果我将运行此脚本我总是得到截断输出,无论我设置初始容量有多高。输出明显被截断,如以下摘录所示

If I am going to run this script I always get a truncated output, no matter how high I may set initial capacity. The output is evidently truncated, as shown in the following excerpt

...
SS0D76I0.cpy
SS0D76M0.cpy
SS0D76N0.cpy
SS

关于全部捕获的任何建议输出命令?该脚本在Windows机器上运行。

Any suggestion about capturing the full output of the command? The script is running on a Windows box.

推荐答案

public void consumeProcessOutput(可附加输出,可附加错误)

获取进程的输出和错误流并读取它们以保持由于完整的输出缓冲区而阻塞的过程。处理后的流数据将附加到提供的Appendable。为此,启动了两个Thread,因此该方法将立即返回。 即使调用waitFor(),线程也不会是join()ed。要等待输出完全消耗,请调用waitForProcessOutput()。

Gets the output and error streams from a process and reads them to keep the process from blocking due to a full output buffer. The processed stream data is appended to the supplied Appendable. For this, two Threads are started, so this method will return immediately. The threads will not be join()ed, even if waitFor() is called. To wait for the output to be fully consumed call waitForProcessOutput().

public void waitForProcessOutput()

获取进程的输出和错误流并读取它们以防止进程因完整输出缓冲区而阻塞。丢弃流数据但避免了由于完整输出缓冲而导致的阻塞。 如果您不关心标准或错误输出并且只希望进程以静默方式运行,请使用此方法 - 但请谨慎使用,因为由于流数据被丢弃,可能很难跟踪出现问题时。为此,启动了两个Thread,但是join()ed,所以我们等待。正如waitFor ...名称所暗示的那样,我们也要等到我们完成。最后,关闭输出和错误流。

Gets the output and error streams from a process and reads them to keep the process from blocking due to a full output buffer. The stream data is thrown away but blocking due to a full output buffer is avoided. Use this method if you don't care about the standard or error output and just want the process to run silently - use carefully however, because since the stream data is thrown away, it might be difficult to track down when something goes wrong. For this, two Threads are started, but join()ed, so we wait. As implied by the waitFor... name, we also wait until we finish as well. Finally, the output and error streams are closed.

public void waitForProcessOutput(Appendable output,Appendable error)

获取进程的输出和错误流并读取它们以防止进程因完整输出缓冲区而阻塞。处理后的流数据将附加到提供的Appendable。为此,启动了两个Thread,但是join()ed,所以我们等待。正如waitFor ...名称所暗示的那样,我们也要等到我们完成。最后,输入,输出和错误流已关闭。

Gets the output and error streams from a process and reads them to keep the process from blocking due to a full output buffer. The processed stream data is appended to the supplied Appendable. For this, two Threads are started, but join()ed, so we wait. As implied by the waitFor... name, we also wait until we finish as well. Finally, the input, output and error streams are closed.

因此,而不是

sproc.consumeProcessOutput(sout, serr)
sproc.waitForProcessOutput()

call

sproc.waitForProcessOutput(sout, serr)

这篇关于无法从groovy执行中捕获完整输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆