``` val bis = from.buffered() var ...
# announcements
x
Copy code
val bis = from.buffered()

        var b : ByteArray = byteArrayOf()
        while ( ( bis.read(b) ) != -1 ) {

        }
is there a better way to read in bytes in kotlin (yes the file is binary)
x
can't use that, this can apparently be up to 256G
a
you can't even do it that way really, you will need the result of
read
(and a capacity on the byte array)
x
@Alan Evans yeah...
Copy code
var bytes = ByteArray(72)
        var read = 0
        while ( ( read = bis.read(bytes) ) != -1 ) {
            for (byte in bytes) {
                addByte( byte )
            }
        }
kotlin doesn't like the assignment there
a
yeah 😞
x
is this some weird edge case where I'm going to have to write some of it in java
a
no no, but it won't look as clean that's all
Copy code
val b = ByteArray(4096)
        do {
            val read = bis.read(b, 0, b.size)
            if (read == -1) break
            // you got "read" bytes in buffer

        } while (true)
a
do while
is the cleanest way I know of
but the
while(true)
is icky
x
yeah... this is a slurp though right, puts everything in the array, which I'm thinking I don't actually want... hmmm.... I think this is enough for me to work with though. Thanks
a
But you can hide it away:
Copy code
fun InputStream.readInBlocks(blockSize: Int = 4096, function: (ByteArray, Int) -> Unit) {
    val buffer = ByteArray(blockSize)
    do {
        val count = read(buffer, 0, blockSize)
        if (count == -1) break
        function(buffer, count)
    } while (true)
}
Usage:
Copy code
bis.readInBlocks { buffer, count ->
            // you got "count" bytes in buffer
        }
👍 1
How do you mean "puts everything in the array"?
x
reads the stream until the stream is empty
I guess it doesn't, that wouldn't make sense
a
ok, and oh and you might actually want to use
inline fun
as that would save a lot of function calls over 256GB of small blocks.
Yes the array gets reused, it's only ever 4096 bytes if that was your confusion.
x
my confusion is it's before 10am and java's io api... 😉 but yes... more caffeine
I don't do a lot of file reading, and just learning kotlin, so thanks 😄
@Alan Evans one last question about this code, the extension method could be used on any inputstream right? do you have any suggestions on code placement for extension methods?
a
Not particularly. I would just say apply same reasoning as any other function: Do you need to access it from multiple classes? then put somewhere they can all access it. If just for one place, you can put at the bottom of that file and keep
private fun
. Remember you can always move later, and I think it's best to keep visibility of anything, class, function or property, to the minimum you need.
p
Okio
x
?
https://stackoverflow.com/q/54229767/206466 hmm... same problem, am I doing something wrong in the loop?
@pp.amorim you have an example of how I might do this with okio? looking at the lib but find its immediate examples opaque for my use case