栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从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。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508870.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号