在Java中,如何使用Google';这是番石榴汁吗?

在Java中,如何使用Google';这是番石榴汁吗?,java,process,inputstream,guava,Java,Process,Inputstream,Guava,我正在执行一个进程,希望将其输出读入字符串。我希望使用CharStreams.toString(InputSupplier),而不是处理try/catch/finally。不幸的是,进程的getInputStream()返回的流类型是InputStream,而不是InputSupplier。我如何使用它来创建一个InputSupplier,用于toString() 理想情况下,我可以这样做: CharStreams.toString(CharStreams.newReaderSupplier(p

我正在执行一个进程,希望将其输出读入字符串。我希望使用
CharStreams.toString(InputSupplier)
,而不是处理try/catch/finally。不幸的是,进程的
getInputStream()
返回的流类型是
InputStream
,而不是InputSupplier。我如何使用它来创建一个
InputSupplier
,用于t
oString()

理想情况下,我可以这样做:

CharStreams.toString(CharStreams.newReaderSupplier(process.getInputStream()))

但是您不能从InputStream构建InputSupplier,我很难找到如何做到这一点

到目前为止,这是我能做的最好的:

String commandOutput = CharStreams.toString(new InputSupplier<InputStreamReader>() {
    public InputStreamReader getInput() throws IOException {
        return new InputStreamReader(process.getInputStream());
    }
});
String commandOutput=CharStreams.toString(新的InputSupplier(){
公共InputStreamReader getInput()引发IOException{
返回新的InputStreamReader(process.getInputStream());
}
});
这个怎么办:

CharStreams.toString(new InputStreamReader(process.getInpusttream()))

它使用的是CharStreams.toString(可读)

我还没有找到一种用番石榴来做这件事的方法。我相信开发商对此有很好的理由。在保持最小值的同时,我得到的最接近的结果是:

 CharStreams.toString(CharStreams.newReaderSupplier(new InputSupplier<InputStream>() {
        @Override
        public InputStream getInput() throws IOException {
            return inputStream;
        }
    }, Charsets.UTF_16));
CharStreams.toString(CharStreams.newReaderSupplier(newInputSupplier)(){
@凌驾
公共InputStream getInput()引发IOException{
返回输入流;
}
},Charsets.UTF_(16));
我使用了CharStreams中的
newReaderSupplier
,因此您不必用
InputStreamReader
包装它

正确的做法——Guava正试图促使您这么做——是将提供InputStream的代码改为提供InputSupplier

这样做的原因是,通过这种方式,Guava获取流,读取字符串并关闭它,在Guava关闭它之后,您不能意外地使用它,因为您从一开始就没有对InputStream的引用。这消除了许多潜在的bug

另一个重载CharStreams.toString(Readable)不会关闭Readable。如果您想拥有自己的、用于关闭输入流的特殊逻辑,这是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的——在我看来,IOUtils更干净一些:

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();
}

这样做并不会在完成时关闭流——它只对InputSupplier执行此操作