Java reads InputStream As string, comparing performance of various methods

Source: Internet
Author: User
Tags stream api

Summarize other answers I found one main ways to does this (see below). and I wrote some performance tests (see results below):

Ways to convert an InputStream to a String:

Using ioutils.tostring (Apache Utils)
String result = ioutils.tostring (InputStream, standardcharsets.utf_8);
Using Charstreams (Guava)
String result = charstreams.tostring (New InputStreamReader (
InputStream, Charsets.utf_8));
Using Scanner (JDK)
Scanner s = new Scanner (inputstream). Usedelimiter ("\\a");
String result = S.hasnext ()? S.next (): "";
The Using Stream Api (Java 8). Warning:this solution convert different line breaks (like \ r \ n) to \ n.
String result = new BufferedReader (new InputStreamReader (InputStream))
. lines (). Collect (collectors.joining ("\ n"));
Using the parallel Stream Api (Java 8). Warning:this solution convert different line breaks (like \ r \ n) to \ n.
String result = new BufferedReader (new InputStreamReader (InputStream)). Lines ()
. Parallel (). Collect (collectors.joining ("\ n"));
Using InputStreamReader and StringBuilder (JDK)
Final int buffersize = 1024;
Final char[] buffer = new Char[buffersize];
Final StringBuilder out = new StringBuilder ();
Reader in = new InputStreamReader (InputStream, "UTF-8");
for (;;) {
int rsz = in.read (buffer, 0, buffer.length);
if (Rsz < 0)
Break
Out.append (buffer, 0, Rsz);
}
return out.tostring ();
Using StringWriter and Ioutils.copy (Apache Commons)
StringWriter writer = new StringWriter ();
Ioutils.copy (InputStream, writer, "UTF-8");
return writer.tostring ();
Using Bytearrayoutputstream and Inputstream.read (JDK)
Bytearrayoutputstream result = new Bytearrayoutputstream ();
byte[] buffer = new byte[1024];
int length;
while (length = inputstream.read (buffer))! =-1) {
Result.write (buffer, 0, length);
}
Return result.tostring ("UTF-8");
Using BufferedReader (JDK). Warning:this solution convert different line breaks (like \n\r) to Line.separator system property (for example, in Window S to "\ r \ n").
String newLine = System.getproperty ("Line.separator");
BufferedReader reader = new BufferedReader (new InputStreamReader (InputStream));
StringBuilder result = new StringBuilder ();
String Line; Boolean flag = false;
while (line = Reader.readline ()) = null) {
Result.append (flag newLine: ""). Append (line);
Flag = true;
}
return result.tostring ();
Using Bufferedinputstream and Bytearrayoutputstream (JDK)
Bufferedinputstream bis = new Bufferedinputstream (InputStream);
Bytearrayoutputstream buf = new Bytearrayoutputstream ();
int result = Bis.read ();
while (Result! =-1) {
Buf.write ((byte) result);
result = Bis.read ();
}
return buf.tostring ();
Using Inputstream.read () and StringBuilder (JDK). Warning:this solution have problem with Unicode, for example with Russian text (work correctly only with non-unicode text)
int ch;
StringBuilder sb = new StringBuilder ();
while ((ch = inputstream.read ())! =-1)
Sb.append ((char) ch);
Reset ();
return sb.tostring ();
Warning:

Solutions 4, 5 and 9 convert different line breaks to one.
Solution can ' t work correctly with Unicode text
Performance tests

Performance tests for small String (length = 175), URL in github (mode = Average time, system = Linux, score 1,343) Best):

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
Performance tests for big String (length = 50100), url in github (mode = Average time, system = Linux, score 200,715 is th e best):

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/op

Java reads InputStream As string, comparing performance of various methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.