谷歌番石榴。废弃 "InputSupplier "和 "OutputSupplier "实用程序后,使用 "ByteSource "和 "ByteSink"。

问题描述 投票:0回答:1

我在我的项目中遇到了一些旧的代码,其中使用了 InputSupplierOutputSupplier 的实用程序。由于这些类在最新的版本中已被废弃,我需要相应地重构我的代码。

通过在网上查找,我知道可以使用 ByteSourceByteSink 代替 InputSupplierOutputSupplier 通过做一些最小的改动。下面的例子代表了我用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
      }

在使用 ByteSink

public static ByteSink newOutputSupplier() {
    return new ByteSink() {
      @Override
      public OutputStream openStream() throws IOException {
        return new FileOutputStream(file); // file defined earlier
      }
    };
  }

类似的重构 InputSupplier 相关代码。

问题1: 以上重构是否正确,我的思路是否正确?

问题二: 我可以包 ZipOutputStreamByteSink ?

问题3: 如果前一个问题的答案是 "是",那么我将如何取回。ZipOutputStreamByteSink 由于它返回 OutputStream? 在这种情况下,是否可以像下面的片段一样进行铸造?

ZipOutputStream zipOutputStream = (ZipOutputStream) newOutputSupplier().openStream(); // Casting

还是我需要创建新的 ZipOutputStreamOutputStream?

ZipOutputStream zipOutputStream = new ZipOutputStream(newOutputSupplier().openStream());

问题4: 我可以包任何类型的 InputStreamOutputStream?

: 我主要举了以下例子 OutputStream, ZipOutputStreamByteSink 但我正在做类似的事情,为 InputStream, ZipInputStreamByteSource.

java guava java-io
1个回答
1
投票
  1. 你可能应该使用例如Files.asByteSink。
  2. 我不会。 ByteSink 是为了通用 OutputStream没有其他方法的。
  3. 不适用。
  4. 是的,只要你每次都能创建一个新的方法。
© www.soinside.com 2019 - 2024. All rights reserved.