Moneem A Abbas
Moneem A Abbas

Reputation: 1

how can i convert InputStream to BufferInputStream in kotlin

How can I convert InputStream to BufferInputStream in Kotlin?

fun ConvertStreamToString(inputStream:InputStream): String {

    val bufferreader=BufferedReader(InputStreamReader(inputStream))

    var line= String
    var AllString:String=""
    try {
        do {
            line=bufferreader.readLine()
            if (line!=null){
                AllString+=line
            }

Upvotes: 0

Views: 1271

Answers (1)

zsmb13
zsmb13

Reputation: 89608

If you have an InputStream, you can create a BufferedInputStream from it by using buffered from the standard library:

val buffered: BufferedInputStream = inputStream.buffered()

If instead you want to create a BufferedReader (as in your code), you can use the bufferedReader extension.

val reader: BufferedReader = inputStream.bufferedReader()

And if you want to read all lines of a BufferedReader, you can do any of the following:

val lineList: List<String> = reader.readLines()

val lineSequence: Sequence<String> = reader.lineSequence()

val linesAsOneString: String = reader.lineSequence().joinToString("\n")

So, for example, you could implement your original function like this:

fun convertStreamToString(inputStream: InputStream) 
        = inputStream.bufferedReader().lineSequence().joinToString(separator = "")

Upvotes: 1

Related Questions