Kotlin Program to Convert InputStream to String

Converting an InputStream to a String is a common task in Kotlin when dealing with input/output operations. It involves reading the bytes from an InputStream and converting them into a readable string format. In this article, we will write three different Kotlin Program to Convert InputStream to String.

1. Using BufferedReader and StringBuilder

1.1 Kotlin Program to Convert InputStream to String

Kotlin
import java.io.*

fun main() {
    val inputStream: InputStream = ByteArrayInputStream("Hello Kotlin!".toByteArray())
    val reader = BufferedReader(InputStreamReader(inputStream))
    val stringBuilder = StringBuilder()
    var line: String?

    while (reader.readLine().also { line = it } != null) {
        stringBuilder.append(line).append("\n")
    }

    val result = stringBuilder.toString()
    println(result)
}

Output:

Kotlin
Hello Kotlin!

Explanation:

In this example, we create an InputStream using ByteArrayInputStream with a sample string. We then use a BufferedReader to read the InputStream line by line and append each line to a StringBuilder. Finally, we convert the StringBuilder contents to a string using toString().

2. Using Scanner and ByteArrayInputStream

2.1 Example Code:

Kotlin
import java.io.*
import java.util.*

fun main() {
    val inputStream: InputStream = ByteArrayInputStream("Kotlin is awesome!".toByteArray())
    val scanner = Scanner(inputStream)
    val result = scanner.nextLine()

    println(result)
}

Output:

Kotlin
Kotlin is awesome!

Explanation:

Here, we utilize a Scanner to read from the InputStream provided by ByteArrayInputStream. We use nextLine() to read the entire input as a single string.

3. Using ByteArrayOutputStream and toString()

3.1 Example Code:

Kotlin
import java.io.*

fun main() {
    val inputStream: InputStream = ByteArrayInputStream("Working with InputStream".toByteArray())
    val outputStream = ByteArrayOutputStream()
    val buffer = ByteArray(1024)
    var length: Int

    while (inputStream.read(buffer).also { length = it } != -1) {
        outputStream.write(buffer, 0, length)
    }

    val result = outputStream.toString()
    println(result)
}

Output:

Kotlin
Working with InputStream

Explanation:

In this example, we read bytes from the InputStream into a ByteArrayOutputStream, and then convert the contents of the ByteArrayOutputStream to a string using toString().

Summary and Conclusion

Converting an InputStream to a String in Kotlin can be achieved using several methods:

  1. Using BufferedReader and StringBuilder for efficient reading and appending.
  2. Utilizing Scanner for simpler input parsing and reading.
  3. Employing ByteArrayOutputStream to buffer and convert bytes to a string.

Each approach has its own advantages and is suitable for different scenarios. Understanding these methods empowers developers to efficiently handle InputStream to String conversions in their Kotlin programs, enhancing the readability and flexibility of their code.