tags:

views:

130

answers:

2

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(...).

A: 
elou
A: 

That would be as wrong as wrapping an Iterator to an Iterable, I feel there is like zero probability of such a thing going into the library. As elou says, you can use ByteStreams.copy() method, but there doesn't seem to be an obvious reason to do equals() on two streams.

I understand guava authors hesitation to add such a (trivial) method - how common can it be to fully (or partially, but without knowing where the stream was left, so it's as good as unusable thereafter) read two streams just to see if they are the same, without any other processing of the data? Do these bytes come from a non-repeatable-read source, like a network socket? Otherwise, if it is just a file somewhere, or an in-memory byte array, there are other ways that lend themselves to do an equality test.

Dimitris Andreou