Stack Overflow上瞅到200万阅读量的一个提问:Java中怎么快速把InputStream转化为String

前言

整好就是一个工具包大满贯啊

正文

就是它

其实我只是偶尔上Stack Overflow,直到看了这个200万次阅读量的提问:

How do I read / convert an InputStream into a String in Java?

我惊呆了!!!

怎么会有这么多人围观。

我第一反应的解决办法是使用Apache commons包的工具类IOUtils,果不其然,第一条回答就是这个。

我的天!居然有2000+的赞!

继续往下看,发现大家的不少的骚操作

使用 CharStreams (Guava)

1
2
String result = CharStreams.toString(new InputStreamReader(
inputStream, Charsets.UTF_8));

使用 Scanner (JDK)

1
2
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
String result = s.hasNext() ? s.next() : "";

使用 Stream API (Java 8).

Warning: This solution converts different line breaks (like \r\n) to \n.

1
2
String result = new BufferedReader(new InputStreamReader(inputStream))
.lines().collect(Collectors.joining("\n"));

使用 parallel Stream API (Java 8).

Warning: This solution converts different line breaks (like \r\n) to \n.

1
2
String result = new BufferedReader(new InputStreamReader(inputStream)).lines()
.parallel().collect(Collectors.joining("\n"));

使用 InputStreamReader and StringBuilder (JDK)

1
2
3
4
5
6
7
8
9
final int bufferSize = 1024;
final char[] buffer = new char[bufferSize];
final StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(stream, StandardCharsets.UTF_8);
int charsRead;
while((charsRead = in.read(buffer, 0, buffer.length)) > 0) {
out.append(buffer, 0, charsRead);
}
return out.toString();

使用 StringWriter and IOUtils.copy (Apache Commons)

1
2
3
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, "UTF-8");
return writer.toString();

使用 ByteArrayOutputStream and inputStream.read (JDK)

1
2
3
4
5
6
7
8
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
// StandardCharsets.UTF_8.name() > JDK 7
return result.toString("UTF-8");

使用 BufferedReader (JDK).

Warning: This solution converts different line breaks (like \n\r) to line.separator system property (for example, in Windows to “\r\n”).

1
2
3
4
5
6
7
8
9
String newLine = System.getProperty("line.separator");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
boolean flag = false;
for (String line; (line = reader.readLine()) != null; ) {
result.append(flag? newLine: "").append(line);
flag = true;
}
return result.toString();

使用 BufferedInputStream and ByteArrayOutputStream (JDK)

1
2
3
4
5
6
7
8
9
BufferedInputStream bis = new BufferedInputStream(inputStream);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
buf.write((byte) result);
result = bis.read();
}
// StandardCharsets.UTF_8.name() > JDK 7
return buf.toString("UTF-8");

使用 inputStream.read() and StringBuilder (JDK).

Warning: This solution has problems with Unicode, for example with Russian text (works correctly only with non-Unicode text)

1
2
3
4
5
6
int ch;
StringBuilder sb = new StringBuilder();
while((ch = inputStream.read()) != -1)
sb.append((char)ch);
reset();
return sb.toString();

甚至,还贴出了微基准测试的结果,果然狠人啊。

对于小字符串(length = 175) 结果如下:
mode = Average Time, system = Linux, score 1,343 is the best :

1
2
3
4
5
6
7
8
9
10
11
12
              Benchmark                         Mode  Cnt   Score   Error  Units
8. ByteArrayOutputStream and read (JDK) avgt 10 1,343 ± 0,028 us/op
6. InputStreamReader and StringBuilder (JDK) avgt 10 6,980 ± 0,404 us/op
10. BufferedInputStream, ByteArrayOutputStream avgt 10 7,437 ± 0,735 us/op
11. InputStream.read() and StringBuilder (JDK) avgt 10 8,977 ± 0,328 us/op
7. StringWriter and IOUtils.copy (Apache) avgt 10 10,613 ± 0,599 us/op
1. IOUtils.toString (Apache Utils) avgt 10 10,605 ± 0,527 us/op
3. Scanner (JDK) avgt 10 12,083 ± 0,293 us/op
2. CharStreams (guava) avgt 10 12,999 ± 0,514 us/op
4. Stream Api (Java 8) avgt 10 15,811 ± 0,605 us/op
9. BufferedReader (JDK) avgt 10 16,038 ± 0,711 us/op
5. parallel Stream Api (Java 8) avgt 10 21,544 ± 0,583 us/op

对于大字符串(length = 50100) 结果如下:
Performance tests for big String (length = 50100)
mode = Average Time, system = Linux, score 200,715 is the best

1
2
3
4
5
6
7
8
9
10
11
12
               Benchmark                        Mode  Cnt   Score        Error  Units
8. ByteArrayOutputStream and read (JDK) avgt 10 200,715 ± 18,103 us/op
1. IOUtils.toString (Apache Utils) avgt 10 300,019 ± 8,751 us/op
6. InputStreamReader and StringBuilder (JDK) avgt 10 347,616 ± 130,348 us/op
7. StringWriter and IOUtils.copy (Apache) avgt 10 352,791 ± 105,337 us/op
2. CharStreams (guava) avgt 10 420,137 ± 59,877 us/op
9. BufferedReader (JDK) avgt 10 632,028 ± 17,002 us/op
5. parallel Stream Api (Java 8) avgt 10 662,999 ± 46,199 us/op
4. Stream Api (Java 8) avgt 10 701,269 ± 82,296 us/op
10. BufferedInputStream, ByteArrayOutputStream avgt 10 740,837 ± 5,613 us/op
3. Scanner (JDK) avgt 10 751,417 ± 62,026 us/op
11. InputStream.read() and StringBuilder (JDK) avgt 10 2919,350 ± 1101,942 us/o

总结

如获宝藏,看来需要时不时的逛逛Stack Overflow了。

转载自:占小狼

以上。

谢谢你请我吃糖果