Is there a concise way to create an InputSupplier for an InputStream in Google Guava?

后端 未结 3 1177
被撕碎了的回忆
被撕碎了的回忆 2021-02-12 22:07

There are a few factory methods in Google Guava to create InputSuppliers, e.g. from a byte[]:

ByteStreams.newInputStreamSupplier(bytes);
         


        
3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-12 22:49

    There's no way to convert an arbitrary InputStream into an InputSupplier, because an InputSupplier is supposed to be an object that can create a fresh, new InputStream every time its getInput() method is called. This is only possible when the underlying source of bytes is available for re-use; hence the factory methods that take a byte[] or File and return an InputSupplier.

    As Dimitris suggests, InputSupplier relates to InputStream in the same way that Iterable relates to Iterator. The anonymous class you describe is incorrect because it returns the same stream every time getInput() is called, so subsequent invocations will return an InputStream that is already exhausted and closed.

    Here is another problem with your anonymous class: part of the motivation for InputSupplier is to limit the visibility of the actual InputStream so that it can be closed automatically. If you wrap an externally-visible InputStream in an InputSupplier and then pass that into a utility method, the utility method may close your InputStream. You might be OK with that but that's not a clean usage pattern that Guava would want to promote.

    When I found myself wanting to do the same thing, I realized I was doing it backwards. Instead of doing this:

    Files.copy(InputSupplier.of(inputStream), destinationFile);
    

    (doesn't exist), I should instead be doing this:

    ByteStreams.copy(inputStream, Files.newOutputStreamSupplier(destinationFile));
    

提交回复
热议问题