xenoterracide
01/15/2019, 2:58 PMval 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)hho
01/15/2019, 3:00 PMxenoterracide
01/15/2019, 3:01 PMAlan Evans
01/15/2019, 3:04 PMread
xenoterracide
01/15/2019, 3:11 PMvar bytes = ByteArray(72)
var read = 0
while ( ( read = bis.read(bytes) ) != -1 ) {
for (byte in bytes) {
addByte( byte )
}
}
kotlin doesn't like the assignment thereAlan Evans
01/15/2019, 3:12 PMxenoterracide
01/15/2019, 3:12 PMAlan Evans
01/15/2019, 3:13 PMval b = ByteArray(4096)
do {
val read = bis.read(b, 0, b.size)
if (read == -1) break
// you got "read" bytes in buffer
} while (true)
do while
is the cleanest way I know ofwhile(true)
is ickyxenoterracide
01/15/2019, 3:17 PMAlan Evans
01/15/2019, 3:18 PMfun 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:
bis.readInBlocks { buffer, count ->
// you got "count" bytes in buffer
}
xenoterracide
01/15/2019, 3:20 PMAlan Evans
01/15/2019, 3:21 PMinline fun
as that would save a lot of function calls over 256GB of small blocks.xenoterracide
01/15/2019, 3:26 PMAlan Evans
01/15/2019, 3:36 PMprivate 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.pp.amorim
01/17/2019, 12:27 PMxenoterracide
01/17/2019, 4:48 PM