如何从java.lang.Process关闭适当的std流?

如何从java.lang.Process关闭适当的std流?,第1张

如何从java.lang.Process关闭适当的std流?

只是让您知道我目前在我们的代码库中:

public static void close(@Nullable Process process) throws IOException {  if (process == null) {    return;  }  Throwable t = null;  try {    flushQuietly(process.getOutputStream());  }  catch (Throwable e) {    t = mostimportantThrowable(t, e);  }  try {    close(process.getOutputStream());  }  catch (Throwable e) {    t = mostimportantThrowable(t, e);  }  try {    skipAllQuietly(null, TIMEOUT, process.getInputStream());  }  catch (Throwable e) {    t = mostimportantThrowable(t, e);  }  try {    close(process.getInputStream());  }  catch (Throwable e) {    t = mostimportantThrowable(t, e);  }  try {    skipAllQuietly(null, TIMEOUT, process.getErrorStream());  }  catch (Throwable e) {    t = mostimportantThrowable(t, e);  }  try {    close(process.getErrorStream());  }  catch (Throwable e) {    t = mostimportantThrowable(t, e);  }  try {    try {      Thread monitor = ThreadMonitor.start(TIMEOUT);      process.waitFor();      ThreadMonitor.stop(monitor);    }    catch (InterruptedException e) {      t = mostimportantThrowable(t, e);      process.destroy();    }  }  catch (Throwable e) {    t = mostimportantThrowable(t, e);  }  if (t != null) {    if (t instanceof Error) {      throw (Error) t;    }    if (t instanceof RuntimeException) {      throw (RuntimeException) t;    }    throw t instanceof IOException ? (IOException) t : new IOException(t);  }}

skipAllQuietly(...)
消耗完整的InputStreams。
org.apache.commons.io.ThreadMonitor
如果超过给定的超时,它在内部使用类似于中断消耗的实现。

mostimportantThrowable(...)
决定应返回哪些Throwable。错误遍及一切。第一次发生的高优先级比后来发生的高。这里没有什么很重要的,因为这些Throwable最有可能在以后被丢弃。我们想继续在这里工作,我们只能扔一个,所以如果有的话,我们必须决定最后扔什么。

close(...)
是用于关闭内容的null安全实现,但是在出现问题时抛出Exception。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5501261.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存