Ways to convert an InputStream to a String
Reference:
[1] https://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
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() : ""; -
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 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); } // StandardCharsets.UTF_8.name() > JDK 7 return result.toString("UTF-8"); -
Using BufferedReader (
JDK). Warning: This solution convert different line breaks (like\n\r) toline.separatorsystem property (for example, in Windows 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(); } // StandardCharsets.UTF_8.name() > JDK 7 return buf.toString("UTF-8"); -
Using inputStream.read() and StringBuilder (
JDK). Warning: This solution has 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,5and9convert different line breaks to one. -
Solution
11can'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 is the 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/
