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