作为读取器打开输入流
我可以使用 Guava 轻松地将 InputStream
转换为 BufferedReader
吗?
我正在寻找类似的东西:
InputStream inputStream = ...;
BufferedReader br = Streams.newBufferedReader(inputStream);
我可以使用Files.newReader(File file, Charset charset)
打开文件。这很酷,我想使用 InputStream
做同样的事情。
更新:
使用 CharStreams.newReaderSupplier
对我来说似乎很冗长。如果我错了,请纠正我,但为了使用 Guava 轻松将 InputStream
转换为 BufferedReader
,我必须做类似的事情:
final InputStream inputStream = new FileInputStream("/etc/fstab");
Reader bufferedReader = new BufferedReader(CharStreams.newReaderSupplier(new InputSupplier<InputStream>(){
public InputStream getInput() throws IOException {
return inputStream;
}
}, Charset.defaultCharset()).getInput());
当然我可以创建助手做某事:
return new BufferedReader(new InputStreamReader(inputStream));
不过我认为这样的助手应该由 Guava IO 提供。我可以为 File 实例做这样的技巧。为什么我不能输入InputStream?
// Guava can do this
Reader r = Files.newReader(new File("foo"), charset);
// but cannot do this
Reader r = SomeGuavaUtil.newReader(inputStream, charset);
如果我错了,请纠正我,但在我看来,API 缺乏。
Can I easily convert InputStream
to BufferedReader
using Guava?
I'm looking for something like:
InputStream inputStream = ...;
BufferedReader br = Streams.newBufferedReader(inputStream);
I can open files using the Files.newReader(File file, Charset charset)
. That's cool and I want to do the same using the InputStream
.
UPDATE:
Using CharStreams.newReaderSupplier
seems to verbose for me. Correct me if I'm wrong, but in order to easily convert InputStream
to BufferedReader
using Guava I have to do something like that:
final InputStream inputStream = new FileInputStream("/etc/fstab");
Reader bufferedReader = new BufferedReader(CharStreams.newReaderSupplier(new InputSupplier<InputStream>(){
public InputStream getInput() throws IOException {
return inputStream;
}
}, Charset.defaultCharset()).getInput());
Of course I can create helper do sth like:
return new BufferedReader(new InputStreamReader(inputStream));
However I think that such helper should be offered by Guava IO. I can do such trick for File instance. Why cannot I for InputStream?
// Guava can do this
Reader r = Files.newReader(new File("foo"), charset);
// but cannot do this
Reader r = SomeGuavaUtil.newReader(inputStream, charset);
Correct me If I'm wrong but it seems to me like lack in the API.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,Guava 中没有类似的东西。
CharStreams
是用于使用Reader
和Writer
的通用类,它有一个对任何类型的
供应商都有用的 方法。代码>InputStream
。显然,您可以只编写 new BufferedReader(new InputStreamReader(in, charset)) 或将其包装在您自己的工厂方法中。
编辑:
是的,当您已经拥有
InputStream
时,您不会想使用InputSupplier
版本。这有点像创建一个实际上只能工作一次的Iterable
是一个坏主意,例如包装现有的Iterator
或Enumeration
> 或类似的东西。一般来说,使用InputSupplier
需要考虑如何进行稍微不同的 I/O,例如将File
视为可以充当供应商的东西FileInputStream。我使用了
InputSupplier
将整个请求包装到服务器并将响应内容作为InputStream
返回,使我能够使用 Guava 实用程序将其复制到文件等 无论如何,我不完全确定为什么
CharStreams
没有从InputStream
创建Reader
的方法,也许除了他们觉得没有必要。您可能想提出一个请求此的问题。No, there isn't anything quite like that in Guava.
CharStreams
is the general class for working withReader
s andWriter
s and it has a methodwhich could be useful with any kind of supplier of
InputStream
s.Obviously, you can just write
new BufferedReader(new InputStreamReader(in, charset))
or wrap that in your own factory method as well.Edit:
Yes, you wouldn't want to use the
InputSupplier
version when you already have anInputStream
. It's sort of like how it's a bad idea to make anIterable
that can actually only work once, such as one that wraps an existingIterator
orEnumeration
or some such. In general, usingInputSupplier
requires thinking about how you do I/O a little different, such as thinking of aFile
as something that can act as a supplier ofFileInputStream
s. I've usedInputSupplier
s that wrap whole requests to a server and return the response content as anInputStream
, enabling me to use Guava utilities to copy that to a file, etc.In any case, I'm not entirely sure why
CharStreams
doesn't have a method to create aReader
from anInputStream
other than perhaps they didn't feel it was needed. You may want to file an issue requesting this.