When I want to load a text file completely … is it...
# random
h
When I want to load a text file completely … is it a good idea to do
Copy code
return myInputStream.bufferedReader().use { it.readText() }
or is
Copy code
return myInputStream.reader().use { it.readText() }
just as efficient?
t
not sure abt this, but curious what’s
measureTimeMillis
shows 🤔
h
I don't think I can do a useful benchmark, it would be completely dominated by the network
My question is basically whether the buffering is useful even if I don't need separate lines
p
Copy code
public fun Reader.readText(): String {
    val buffer = StringWriter()
    copyTo(buffer)
    return buffer.toString()
}
public fun Reader.copyTo(out: Writer, bufferSize: Int = DEFAULT_BUFFER_SIZE): Long {
    var charsCopied: Long = 0
    val buffer = CharArray(bufferSize)
    var chars = read(buffer)
    while (chars >= 0) {
        out.write(buffer, 0, chars)
        charsCopied += chars
        chars = read(buffer)
    }
    return charsCopied
}
For just copy no need bufferedReader
BufferedReader is needed when taking char by char from it. Copy is taking by arrays
It can be even worse with BufferedReader cause of one more copy from origin to buffer and then to array