简体   繁体   English

是否有简洁的方法为Google Guava中的InputStream创建InputSupplier?

[英]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所说, InputSupplierIterableIterator相关的方式与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 见: 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.

相关问题 Google guava:在弃用“InputSupplier”和“OutputSupplier”实用程序后使用“ByteSource”和“ByteSink” - Google guava: Using `ByteSource` and `ByteSink` after deprecation of `InputSupplier` and `OutputSupplier` utilities 如何使用Guava的InputSupplier或OutputSupplier api? - how to use the InputSupplier or OutputSupplier api of Guava? 有什么方法可以使用Guava获取InputStream的哈希码吗? - Is there any way to get the hashcode of an InputStream using Guava? 基于String是否为空而创建Guava的Optional的简明方法 - Concise way of creating Guava's Optional based on String being empty or not 为什么Guava InputSupplier / OutputSupplier不扩展Supplier接口? - Why Guava InputSupplier/OutputSupplier does not extend the Supplier interface? 紧凑的方式来创建番石榴Multimaps? - Compact way to create Guava Multimaps? 更新缓存的最佳方法:Google Guava - Best way to update Cache:Google Guava 在 Java 中,如何使用 Google 的 guava CharStreams.toString 进程中的 inputStream? - In Java, how can I use the inputStream from a process with Google's guava CharStreams.toString? 从OutputStream创建InputStream的最有效方法 - Most efficient way to create InputStream from OutputStream 相当于IOUtils.toString(InputStream)的番石榴 - Guava equivalent for IOUtils.toString(InputStream)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM