Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么Guava InputSupplier/OutputSupplier不扩展供应商接口?_Java_Guava - Fatal编程技术网

Java 为什么Guava InputSupplier/OutputSupplier不扩展供应商接口?

Java 为什么Guava InputSupplier/OutputSupplier不扩展供应商接口?,java,guava,Java,Guava,我的用例是能够在不创建文件的情况下创建FileOutputStream。 因此,我创建了这个基于番石榴的输出流: public class LazyInitOutputStream extends OutputStream { private final Supplier<OutputStream> lazyInitOutputStreamSupplier; public LazyInitOutputStream(Supplier<OutputStream>

我的用例是能够在不创建文件的情况下创建FileOutputStream。 因此,我创建了这个基于番石榴的输出流:

public class LazyInitOutputStream extends OutputStream {

  private final Supplier<OutputStream> lazyInitOutputStreamSupplier;

  public LazyInitOutputStream(Supplier<OutputStream> outputStreamSupplier) {
    this.lazyInitOutputStreamSupplier = Suppliers.memoize(outputStreamSupplier);
  }

  @Override
  public void write(int b) throws IOException {
    lazyInitOutputStreamSupplier.get().write(b);
  }

  @Override
  public void write(byte b[]) throws IOException {
    lazyInitOutputStreamSupplier.get().write(b);
  }

  @Override
  public void write(byte b[], int off, int len) throws IOException {
    lazyInitOutputStreamSupplier.get().write(b,off,len);
  }


  public static LazyInitOutputStream lazyFileOutputStream(final File file) {
    return lazyFileOutputStream(file,false);
  }

  public static LazyInitOutputStream lazyFileOutputStream(final File file,final boolean append) {
    return new LazyInitOutputStream(new Supplier<OutputStream>() {
      @Override
      public OutputStream get() {
        try {
          return new FileOutputStream(file,append);
        } catch (FileNotFoundException e) {
          throw Throwables.propagate(e);
        }
      }
    });
  }

}
有没有一种方法我可以使用OutputSupplier,它会比我现在的代码更优雅


OutputSupplier没有实现Supplier有什么原因吗?

InputSupplier
可以抛出
IOException
,而
Supplier
不能

public static OutputSupplier<FileOutputStream> newOutputStreamSupplier(File file,
                                                       boolean append)