[英]Is there a concise way to create an InputSupplier for an InputStream in Google Guava?
There are a few factory methods in Google Guava to create InputSuppliers, eg from a byte[]
: Google Guava中有一些工厂方法可以创建InputSuppliers,例如从
byte[]
:
ByteStreams.newInputStreamSupplier(bytes);
Or from a File
: 或者来自
File
:
Files.newInputStreamSupplier(file);
Is there a similar way to to create an InputSupplier
for a given InputStream
? 是否有类似的方法为给定的
InputStream
创建InputSupplier
?
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 eg Files.copy(...)
or ByteStreams.equal(...)
. 背景:我想将InputStreams与例如
Files.copy(...)
或ByteStreams.equal(...)
。
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. 没有办法将任意
InputStream
转换为InputSupplier<InputStream>
,因为InputSupplier<InputStream>
应该是一个对象,每次调用其getInput()
方法时都可以创建一个全新的InputStream
。 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>
. 因此采用
byte[]
或File
并返回InputSupplier<InputStream>
的工厂方法。
As Dimitris suggests, InputSupplier
relates to InputStream
in the same way that Iterable
relates to Iterator
. 正如Dimitris所说,
InputSupplier
与Iterable
与Iterator
相关的方式与InputStream
相关。 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. 您描述的匿名类是不正确的,因为每次调用
getInput()
时它都返回相同的流,因此后续调用将返回已经用尽和关闭的InputStream
。
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. 这是您的匿名类的另一个问题:
InputSupplier
一部分动机是限制实际InputStream
的可见性,以便它可以自动关闭。 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
. 如果在
InputSupplier
包装外部可见的InputStream
,然后将其传递给实用程序方法,则实用程序方法可能会关闭InputStream
。 You might be OK with that but that's not a clean usage pattern that Guava would want to promote. 您可能对此感到满意,但这不是Guava想要推广的干净使用模式。
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));
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. 将输入流存储在字节数组或文件中并使用ByteStreams.newInputStreamSupplier()或Files.newInputStreamSupplier()创建供应商的唯一选择,但我不鼓励这样做。
You could also use 你也可以用
public static long copy(InputStream from, OutputStream to)
from
ByteStreams
see: src
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. 这就像将Iterator包装到Iterable一样错误,我觉得这样的东西进入库的可能性为零。 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.
正如elou所说,你可以使用ByteStreams.copy()方法,但似乎没有明显的理由在两个流上做equals()。
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.
否则,如果它只是某个文件或内存中的字节数组,还有其他方法可以进行相等性测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.