[英]Google guava: Using `ByteSource` and `ByteSink` after deprecation of `InputSupplier` and `OutputSupplier` utilities
I came across old code in my project which uses InputSupplier
and OutputSupplier
utility from Google Guava.我在我的项目中遇到了旧代码,它使用来自 Google Guava 的
InputSupplier
和OutputSupplier
实用程序。 Since these classes are deprecated in the latest version, I need to refactor my code accordingly.由于这些类在最新版本中已弃用,因此我需要相应地重构我的代码。
By looking on the inte.net I came to know that I can use ByteSource
and ByteSink
in place of InputSupplier
and OutputSupplier
by doing some minimal changes.通过查看 inte.net,我了解到我可以使用
ByteSource
和ByteSink
来代替InputSupplier
和OutputSupplier
,只需做一些最小的更改。 The below sample represents a case where I have used ByteSink in place of OutputSupplier
.下面的示例代表我使用 ByteSink 代替
OutputSupplier
的情况。
public static OutputSupplier<? extends OutputStream> newOutputSupplier() {
return new OutputSupplier<OutputStream>() {
@Override
public OutputStream getOutput() throws IOException {
return new FileOutputStream(file); // file defined earlier
}
After using ByteSink
使用
ByteSink
后
public static ByteSink newOutputSupplier() {
return new ByteSink() {
@Override
public OutputStream openStream() throws IOException {
return new FileOutputStream(file); // file defined earlier
}
};
}
Similar refactoring for InputSupplier
related code. InputSupplier
相关代码的类似重构。
Question 1 : Is the above refactoring is correct?问题一:以上重构是否正确? am I thinking in the right direction?
我在想正确的方向吗?
Question 2 : Can i wrap ZipOutputStream
in ByteSink
?问题 2 :我可以将
ZipOutputStream
包装在ByteSink
中吗?
Question 3 : If yes for the previous question, how will I get back the ZipOutputStream
from ByteSink
as it returns OutputStream
?问题 3 :如果上一个问题的答案是肯定的,那么当
ByteSink
返回OutputStream
时,我将如何取回ZipOutputStream
? Will casting work in this case as in below snippet?在这种情况下,铸造工作会像下面的代码片段那样吗?
ZipOutputStream zipOutputStream = (ZipOutputStream) newOutputSupplier().openStream(); // Casting
Or do I need to create new ZipOutputStream
from OutputStream
?或者我需要从
OutputStream
创建新的ZipOutputStream
吗?
ZipOutputStream zipOutputStream = new ZipOutputStream(newOutputSupplier().openStream());
Question 4 : Can I wrap any kind of InputStream
or OutputStream
?问题 4 :我可以包装任何类型的
InputStream
或OutputStream
吗?
NOTE : I have mainly given example of OutputStream
, ZipOutputStream
and ByteSink
but I am doing the similar things for InputStream
, ZipInputStream
and ByteSource
.注意:我主要给出了
OutputStream
、 ZipOutputStream
和ByteSink
的示例,但我正在为InputStream
、 ZipInputStream
和ByteSource
做类似的事情。
ByteSink
is intended for generic OutputStream
s that don't have additional methods. ByteSink
适用于没有附加方法的通用OutputStream
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.