[英]In Java, how can I use the inputStream from a process with Google's guava CharStreams.toString?
我正在执行一个进程,并希望将其 output 读入字符串。 我希望使用Guava CharStreams.toString(InputSupplier<R> supplier)
而不是处理 try/catch/finally。 不幸的是,Process 的getInputStream()
返回的 stream 是InputStream
类型,而不是 InputSupplier。 如何使用它来创建InputSupplier
以与 t oString()
一起使用?
理想情况下,我可以做这样的事情:
CharStreams.toString(CharStreams.newReaderSupplier(process.getInputStream()))
但是您不能从 InputStream 构造 InputSupplier,而且我很难找出如何做到这一点。
我还没有找到用番石榴做到这一点的方法。 我相信开发人员有充分的理由这样做。 在保持最小化的情况下,我得到的最接近的是:
CharStreams.toString(CharStreams.newReaderSupplier(new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return inputStream;
}
}, Charsets.UTF_16));
我使用newReaderSupplier
的 newReaderSupplier ,所以你不必用InputStreamReader
包装它。
到目前为止,这是我能做的最好的:
String commandOutput = CharStreams.toString(new InputSupplier<InputStreamReader>() {
public InputStreamReader getInput() throws IOException {
return new InputStreamReader(process.getInputStream());
}
});
那这个呢:
CharStreams.toString(new InputStreamReader(process.getInpusttream()))
它正在使用CharStreams.toString(Readable)
。
正确的做法——以及 Guava 试图推动你做的事情——是更改为您提供 InputStream 的代码,以改为为您提供 InputSupplier。
原因是这样,Guava 得到 stream,读取字符串,然后关闭它,在 Guava 关闭它之后你不能不小心使用它,因为你从来没有对 InputStream 的引用。 这消除了许多潜在的错误。
另一个重载 CharStreams.toString(Readable) 不会关闭 Readable。 如果您想拥有自己的特殊逻辑来关闭输入 stream,这是 Guava 让您这样做的方式。
这应该这样做:
InputStream is = process.getInputStream();
String content = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
is.close();
这是完整用法的真实示例:
HttpURLConnection connection = null;
URL url;
InputStream is = null;
try {
url = new URL("https://graph.facebook.com/oauth/access_token?client_id=" + appId + "&client_secret=" + appSecret + "&grant_type=client_credentials");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("accept-encoding", "gzip");
is = connection.getInputStream();
String content = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
String[] tokens = content.split("=");
System.out.println(tokens[1]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is != null) try {
is.close();
} catch (IOException e) {}
connection.disconnect();
}
我知道这并没有真正的问题,但为了比较起见,他是你用 IOUtils 做的方式——IMO 更干净一点:
HttpURLConnection connection = null;
URL url;
InputStream is = null;
try {
url = new URL("https://graph.facebook.com/oauth/access_token?client_id=" + appId + "&client_secret=" + appSecret + "&grant_type=client_credentials");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("accept-encoding", "gzip");
is = connection.getInputStream();
String value = IOUtils.toString(is);
if (!Strings.isNullOrEmpty(value)) {
String[] splits = value.split("=");
System.out.println(splits[1]);
}
} catch (IOException e) {
} finally {
IOUtils.closeQuietly(is);
connection.disconnect();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.