有没有简洁的方法为Google Guava中的InputStream创建InputSupplier? [英] Is there a concise way to create an InputSupplier for an InputStream in Google Guava?

查看:598
本文介绍了有没有简洁的方法为Google Guava中的InputStream创建InputSupplier?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Guava中有一些工厂方法可用于创建InputSuppliers,例如来自 byte []

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

ByteStreams.newInputStreamSupplier(bytes);

或者来自文件

Files.newInputStreamSupplier(file);

是否有类似的方法来创建 InputSupplier 对于给定的 InputStream

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

背景:我想将InputStream用于例如 Files.copy(...) ByteStreams.equal(...)

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

推荐答案

不,我什么都没看到。
我认为你找到了最好的方法。
唯一的选择在哪里存放在字节数组或文件中输入stream并使用ByteStreams.newInputStreamSupplier()或Files.newInputStreamSupplier()创建一个Supplier,但我不鼓励这样做。

你也可以使用

No, I haven't seen anything.
I think you have found the best way.
The only alternative where to store the inputstream in a byte array or a file and create a Supplier with ByteStreams.newInputStreamSupplier() or Files.newInputStreamSupplier(), but I would discourage to do like that.
You could also use

public static long copy(InputStream from, OutputStream to)

from

ByteStreams


请参阅: src

这篇关于有没有简洁的方法为Google Guava中的InputStream创建InputSupplier?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆