相当于IOUtils.toString(InputStream)的番石榴 [英] Guava equivalent for IOUtils.toString(InputStream)

查看:3867
本文介绍了相当于IOUtils.toString(InputStream)的番石榴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apache Commons IO 有一个很好的方便方法 IOUtils.toString()读取 InputStream 到一个字符串。

Apache Commons IO has a nice convenience method IOUtils.toString() to read an InputStream to a String.

因为我试图从Apache Commons转移到番石榴:番石榴中是否有相同的东西?我查看了 com.google.common.io 包中的所有类,我几乎找不到任何简单的东西。

Since I am trying to move away from Apache Commons and to Guava: is there an equivalent in Guava? I looked at all classes in the com.google.common.io package and I couldn't find anything nearly as simple.

编辑:我理解并了解charsets的问题。只是碰巧我知道我的所有源都是ASCII(是的,ASCII,而不是ANSI等),所以在这种情况下,编码对我来说不是问题。

I understand and appreciate the issues with charsets. It just so happens that I know that all my sources are in ASCII (yes, ASCII, not ANSI etc.), so in this case, encoding is not an issue for me.

推荐答案

您在对Calum的回答中表示您将要使用

You stated in your comment on Calum's answer that you were going to use

CharStreams.toString(new InputStreamReader(supplier.get(), Charsets.UTF_8))

此代码是有问题的,因为重载 CharStreams.toString(可读)表示:

This code is problematic because the overload CharStreams.toString(Readable) states:


不关闭可读

这意味着你的 InputStreamReader ,以及 supplier.get()返回的 InputStream ,不会在此代码完成后关闭。

This means that your InputStreamReader, and by extension the InputStream returned by supplier.get(), will not be closed after this code completes.

另一方面,如果您利用了似乎已经有 InputSupplier< InputStream> 并使用了重载 CharStreams。 toString(InputSupplier< R extends readable&可关闭> ), toString 方法将处理 Reader 的创建和关闭对于你。

If, on the other hand, you take advantage of the fact that you appear to already have an InputSupplier<InputStream> and used the overload CharStreams.toString(InputSupplier<R extends Readable & Closeable>), the toString method will handle both the creation and closing of the Reader for you.

这正是Jon Skeet所建议的,除了 CharStreams.newReaderSupplier InputStream 作为输入...你必须给它一个 InputSupplier

This is exactly what Jon Skeet suggested, except that there isn't actually any overload of CharStreams.newReaderSupplier that takes an InputStream as input... you have to give it an InputSupplier:

InputSupplier<? extends InputStream> supplier = ...
InputSupplier<InputStreamReader> readerSupplier = 
    CharStreams.newReaderSupplier(supplier, Charsets.UTF_8);

// InputStream and Reader are both created and closed in this single call
String text = CharStreams.toString(readerSupplier);

InputSupplier 的目的是让你的通过允许Guava处理需要丑陋的 try-finally 块的部分来确保资源正确关闭,生活更轻松。

The point of InputSupplier is to make your life easier by allowing Guava to handle the parts that require an ugly try-finally block to ensure that resources are closed properly.

编辑:就个人而言,我发现以下内容(这就是我实际编写它的方式,只是打破了上面代码中的步骤)

Personally, I find the following (which is how I'd actually write it, was just breaking down the steps in the code above)

String text = CharStreams.toString(
    CharStreams.newReaderSupplier(supplier, Charsets.UTF_8));

比这更简洁:

String text;
InputStreamReader reader = new InputStreamReader(supplier.get(), 
    Charsets.UTF_8);
boolean threw = true;
try {
  text = CharStreams.toString(reader);
  threw = false;
}
finally {
  Closeables.close(reader, threw);
}

这或多或少是为了正确处理这个问题而必须写的你自己。

Which is more or less what you'd have to write to handle this properly yourself.

编辑:2014年2月

InputSupplier OutputSupplier 并且在Guava 16.0中不推荐使用它们的方法。他们的替换是 ByteSource CharSource ByteSink CharSink 。给定 ByteSource ,您现在可以将其内容作为 String 获取,如下所示:

InputSupplier and OutputSupplier and the methods that use them have been deprecated in Guava 16.0. Their replacements are ByteSource, CharSource, ByteSink and CharSink. Given a ByteSource, you can now get its contents as a String like this:

ByteSource source = ...
String text = source.asCharSource(Charsets.UTF_8).read();

这篇关于相当于IOUtils.toString(InputStream)的番石榴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆