Avatar ·

How to read all contents of an InputStream into a string in Kotlin?

Hello everyone! I recently started learning Kotlin and encountered this task: I need to read data from an InputStream (for example, from a file or network response) and convert it into a regular String. The problem is that I'm used to Java, where you need to write a lot of boilerplate code with loops, StringBuilder, and buffers. In Kotlin, there seem to be more concise ways, but I can't find a clear example. Here's what I tried: ```kotlin fun readFromStream(stream: InputStream): String { val bytes = stream.readBytes() return String(bytes) } ``` But this causes confusion with encoding, and I'm not sure if it's correct for large streams. I also heard about `.bufferedReader()` and `.readText()`, but I don't understand how to apply them properly. I need a solution that: 1. Works