title: In Kotlin, how do I read the entire contents of an InputStream into a String?
body: <p>I recently saw code for reading entire contents of an <code>InputStream</code> into a String in Kotlin, such as:</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>// input is of type InputStream
  val baos = ByteArrayOutputStream()
  input.use { it.copyTo(baos) }
  val inputAsString = baos.toString()
  </code></pre>
  <p>And also:</p>
  <pre class="lang-kotlin prettyprint-override"><code>val reader = BufferedReader(InputStreamReader(input))
  try {
      val results = StringBuilder()
      while (true) { 
          val line = reader.readLine()
          if (line == null) break
          results.append(line) 
      }
      val inputAsString = results.toString()
  } finally {
      reader.close()
  }
  </code></pre>
  <p>And even this that looks smoother since it auto-closes the <code>InputStream</code>:</p>
  <pre class="lang-kotlin prettyprint-override"><code>val inputString = BufferedReader(InputStreamReader(input)).useLines { lines -&gt;
      val results = StringBuilder()
      lines.forEach { results.append(it) }
      results.toString()
  }
  </code></pre>
  <p>Or slight variation on that one:</p>
  <pre class="lang-kotlin prettyprint-override"><code>val results = StringBuilder()
  BufferedReader(InputStreamReader(input)).forEachLine { results.append(it) }
  val resultsAsString = results.toString()   
  </code></pre>
  <p>Then this functional fold thingy:</p>
  <pre class="lang-kotlin prettyprint-override"><code>val inputString = input.bufferedReader().useLines { lines -&gt;
      lines.fold(StringBuilder()) { buff, line -&gt; buff.append(line) }.toString()
  }
  </code></pre>
  <p>Or a <strong>bad</strong> variation which doesn't close the <code>InputStream</code>:</p>
  <pre class="lang-kotlin prettyprint-override"><code>val inputString = BufferedReader(InputStreamReader(input))
          .lineSequence()
          .fold(StringBuilder()) { buff, line -&gt; buff.append(line) }
          .toString()
  </code></pre>
  <p>But they are all clunky and I keep finding newer and different versions of the same... and some of them never even close the <code>InputStream</code>.  What is a non-clunky (idiomatic) way to read the <code>InputStream</code>?</p>
  <p><strong>Note:</strong> <em>this question is intentionally written and answered by the author (<a href="https://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/">Self-Answered Questions</a>), so that the idiomatic answers to commonly asked Kotlin topics are present in SO.</em></p>