Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

io

guava

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

ByteStreams.newInputStreamSupplier(bytes);

Or from a File:

Files.newInputStreamSupplier(file);

Is there a similar way to to create an InputSupplier for a given InputStream?

That is, a way that's more concise than an anonymous class:

new InputSupplier<InputStream>() {
    public InputStream getInput() throws IOException {
        return inputStream;
    }
};

Background: I'd like to use InputStreams with e.g. Files.copy(...) or ByteStreams.equal(...).

like image 577
Fabian Steeg Avatar asked Mar 02 '10 13:03

Fabian Steeg


1 Answers

There's no way to convert an arbitrary InputStream into an InputSupplier<InputStream>, because an InputSupplier<InputStream> 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<InputStream>.

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));
like image 196
jbyler Avatar answered Oct 06 '22 06:10

jbyler