简体   繁体   English

如何使用Guava的InputSupplier或OutputSupplier api?

[英]how to use the InputSupplier or OutputSupplier api of Guava?

I am new to the guava library, and I am quite confused with the InputSupplier and OutputSupplier. 我是guava库的新手,我对InputSupplier和OutputSupplier很困惑。 According to the javadoc, they are just factories for InputStream and OutputStream respectively. 根据javadoc,它们分别只是InputStream和OutputStream的工厂。 However, I don't see anything quite useful of these two interface, could anyone show me a example why I should use these two APIs besides for higher abstraction? 但是,我没有看到这两个接口有什么用处,有没有人能告诉我为什么我应该使用这两个API除了更高的抽象?

The main benefit of both of these interfaces is that they allow library code to control the whole lifecycle of the actual input/output objects. 这两个接口的主要好处是它们允许库代码控制实际输入/输出对象的整个生命周期。 Guava's utilities never close an InputStream or OutputStream that you pass in directly, because that might not be what you want to happen. Guava的实用程序永远不会关闭您直接传入的InputStreamOutputStream ,因为这可能不是您想要发生的。 Even if they did, you'd still need try / finally to deal with errors creating objects. 即使他们这样做了,你仍然需要try / finally处理创建对象的错误。 The suppliers are a sort of lazy callback that allow the actual objects to be created, used and closed all in one go without you having to do a lot of ugly try / finally and error handling. 供应商是一种懒惰的回调,它允许一次性创建,使用和关闭实际对象,而不必进行大量丑陋的try / finally和错误处理。

For an example, just look at the code it takes to copy one file to another (with the actual copying and stream closing code minimized using Guava utilities): 例如,只需查看将一个文件复制到另一个文件所需的代码(使用Guava实用程序将实际复制和流关闭代码最小化):

File in = ...
File out = ...
FileInputStream inStream = new FileInputStream(in);
boolean threw = true;
try {
  /*
   * Note how two try/finally blocks are needed here, in case creating 
   * outStream fails.
   */
  FileOutputStream outStream = new FileOutputStream(out);
  try {
    ByteStreams.copy(inStream, outStream);
    threw = false;
  } finally {
    Closeables.close(outStream, threw);
  }
} finally {
  Closeables.close(inStream, threw);
}

Then look at the code if you use suppliers instead: 如果您使用供应商,请查看代码:

File in = ...
File out = ...
ByteStreams.copy(Files.newInputStreamSupplier(in), 
    Files.newOutputStreamSupplier(out));

With Guava's InputSupplier / OutputSupplier, you do not have to handle yourself the various IOExceptions thrown when instantiating FileInputStreams / FileOutputStreams. 使用Guava的InputSupplier / OutputSupplier,您不必自己处理实例化FileInputStreams / FileOutputStreams时抛出的各种IOExceptions。 Guava will automatically handle these exceptions for you when it calls the InputSupplier.getInput() / OutputSupplier.getOutput() factory methods. 当Guava调用InputSupplier.getInput()/ OutputSupplier.getOutput()工厂方法时,它会自动为您处理这些异常。

By encapsulating the input / output construction in these factory interfaces, you defer their instantiation, and thus defer the moment you will need to handle the IOException / FileNotFoundException they may throw. 通过在这些工厂接口中封装输入/输出结构,您可以推迟实例化,从而推迟处理它们可能抛出的IOException / FileNotFoundException。 In fact, you defer it so much, that it's Guava that handles it for you. 事实上,你推迟了它,它是为你处理它的番石榴。

You can then replace 然后你可以更换

    FileInputStream inputStream = null;
    try {
        inputStream = new FileInputStream(file);
    } catch (FileNotFoundException ex) {
        throw new RuntimeException(ex);
    }

    doSomething(inputStream);

with

    InputSupplier<FileInputStream> inputStreamSupplier = Files.newInputStreamSupplier(file);

    doSomething(inputStreamSupplier);

Edit: also see ColinD's answer, on how Guava may close these inputs / outputs for you when it controls their whole lifecycle (that is, when it used an InputSupplier / OutputSupplier to obtain them). 编辑:另请参阅ColinD的答案,了解Guava如何在控制整个生命周期(即使用InputSupplier / OutputSupplier获取它们时)为您关闭这些输入/输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM