[英]Why Guava InputSupplier/OutputSupplier does not extend the Supplier interface?
My usecase is to be able to create a FileOutputStream without creating the file. 我的用例是能够创建FileOutputStream而不创建文件。 So I have created this Guava based OutputStream: 所以我创建了这个基于番石榴的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);
}
}
});
}
}
This works fine but I saw there were InputSupplier/OutputSupplier
interfaces which I could have used... Except they do not extend Supplier so I can't use the memoization feature which is needed here because I don't want the OutputSupplier
to behave like a factory. 这个工作正常,但是我看到我可以使用InputSupplier/OutputSupplier
接口...除非它们不扩展Supplier,所以我不能使用此处需要的OutputSupplier
功能,因为我不希望OutputSupplier
表现得像一个工厂。
In addition there is the Files
api: 此外,还有Files
api:
public static OutputSupplier<FileOutputStream> newOutputStreamSupplier(File file,
boolean append)
Is there a way I could use OutputSupplier and it would be more elegant than my current code? 有没有一种方法可以使用OutputSupplier,它会比我当前的代码更优雅?
Is there a reason why OutputSupplier doesn't implement Supplier? 为什么OutputSupplier无法实现Supplier?
InputSupplier
可以引发IOException
,而Supplier
不能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.