作为读取器打开输入流

发布于 2024-09-08 02:51:42 字数 1251 浏览 4 评论 0原文

我可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光沙漏 2024-09-15 02:51:42

不,Guava 中没有类似的东西。 CharStreams 是用于使用 ReaderWriter 的通用类,它有一个

InputSupplier<InputStreamReader> newReaderSupplier(
    InputSupplier<? extends InputStream> in, Charset charset)

对任何类型的 供应商都有用的 方法。代码>InputStream

显然,您可以只编写 new BufferedReader(new InputStreamReader(in, charset)) 或将其包装在您自己的工厂方法中。

编辑:

是的,当您已经拥有 InputStream 时,您不会想使用 InputSupplier 版本。这有点像创建一个实际上只能工作一次的 Iterable 是一个坏主意,例如包装现有的 IteratorEnumeration > 或类似的东西。一般来说,使用 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 with Readers and Writers and it has a method

InputSupplier<InputStreamReader> newReaderSupplier(
    InputSupplier<? extends InputStream> in, Charset charset)

which could be useful with any kind of supplier of InputStreams.

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 an InputStream. It's sort of like how it's a bad idea to make an Iterable that can actually only work once, such as one that wraps an existing Iterator or Enumeration or some such. In general, using InputSupplier requires thinking about how you do I/O a little different, such as thinking of a File as something that can act as a supplier of FileInputStreams. I've used InputSuppliers that wrap whole requests to a server and return the response content as an InputStream, 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 a Reader from an InputStream other than perhaps they didn't feel it was needed. You may want to file an issue requesting this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文