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

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

Problem

If you have a java.io.InputStream object, how should you process that object and produce a String?

Suppose I have an InputStream that contains text data, and I want to convert it to a String, so for example I can write that to a log file. What is the easiest way to take the InputStream and convert it to a String?

public String convertStreamToString(InputStream is) {
    // ???
}

Solution

Ways to convert an InputStream to a String:

  1. Using IOUtils.toString (Apache Utils)
     String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
    
  2. Using CharStreams (Guava)
     String result = CharStreams.toString(new InputStreamReader(
           inputStream, Charsets.UTF_8));
    
  3. Using Scanner (JDK)
     Scanner s = new Scanner(inputStream).useDelimiter("\\A");
     String result = s.hasNext() ? s.next() : "";
    
  4. Using Stream API. Warning: This solution converts different line breaks (like \r\n) to \n.
     String result = new BufferedReader(new InputStreamReader(inputStream))
       .lines().collect(Collectors.joining("\n"));
    
  5. Using parallel Stream API . Warning: This solution converts different line breaks (like \r\n) to \n.
     String result = new BufferedReader(new InputStreamReader(inputStream))
        .lines().parallel().collect(Collectors.joining("\n"));
    
  6. Using InputStreamReader and StringBuilder (JDK)
     int bufferSize = 1024;
     char[] buffer = new char[bufferSize];
     StringBuilder out = new StringBuilder();
     Reader in = new InputStreamReader(stream, StandardCharsets.UTF_8);
     for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
         out.append(buffer, 0, numRead);
     }
     return out.toString();
    
  7. Using StringWriter and IOUtils.copy (Apache Commons)
     StringWriter writer = new StringWriter();
     IOUtils.copy(inputStream, writer, "UTF-8");
     return writer.toString();
    
  8. Using ByteArrayOutputStream and inputStream.read (JDK)
     ByteArrayOutputStream result = new ByteArrayOutputStream();
     byte[] buffer = new byte[1024];
     for (int length; (length = inputStream.read(buffer)) != -1; ) {
         result.write(buffer, 0, length);
     }
     // StandardCharsets.UTF_8.name() 
     return result.toString("UTF-8");
    
  9. Using BufferedReader (JDK). Warning: This solution converts different line breaks (like \n\r) to line.separator system 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();
     for (String line; (line = reader.readLine()) != null; ) {
         if (result.length() > 0) {
             result.append(newLine);
         }
         result.append(line);
     }
     return result.toString();
    
  10. Using BufferedInputStream and ByteArrayOutputStream (JDK)
    BufferedInputStream bis = new BufferedInputStream(inputStream);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    for (int result = bis.read(); result != -1; result = bis.read()) {
        buf.write((byte) result);
    }
    // StandardCharsets.UTF_8.name()
    return buf.toString("UTF-8");
    
  11. Using inputStream.read() and StringBuilder (JDK). Warning: This solution has problems with Unicode, for example with Russian text (works correctly only with non-Unicode text)
    StringBuilder sb = new StringBuilder();
    for (int ch; (ch = inputStream.read()) != -1; ) {
        sb.append((char) ch);
    }
    return sb.toString();
    
  12. Share this Post