简体   繁体   English

Google guava:在弃用“InputSupplier”和“OutputSupplier”实用程序后使用“ByteSource”和“ByteSink”

[英]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 的InputSupplierOutputSupplier实用程序。 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,我了解到我可以使用ByteSourceByteSink来代替InputSupplierOutputSupplier ,只需做一些最小的更改。 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 :我可以包装任何类型的InputStreamOutputStream吗?

NOTE : I have mainly given example of OutputStream , ZipOutputStream and ByteSink but I am doing the similar things for InputStream , ZipInputStream and ByteSource .注意我主要给出了OutputStreamZipOutputStreamByteSink的示例,但我正在为InputStreamZipInputStreamByteSource做类似的事情。

  1. You should probably use eg Files.asByteSink.您可能应该使用例如 Files.asByteSink。
  2. I wouldn't.我不会。 ByteSink is intended for generic OutputStream s that don't have additional methods. ByteSink适用于没有附加方法的通用OutputStream
  3. Not applicable.不适用。
  4. Yes, as long as you can create a fresh one each time.是的,只要您每次都能创建一个新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM