elect
09/19/2017, 8:40 AMByteBuffer
to file, what's the best way? Using InputStream
like here: https://stackoverflow.com/questions/35528409/write-a-large-inputstream-to-file-in-kotlin?diesieben07
09/19/2017, 1:11 PMfun Path.channel(vararg options: OpenOption): FileChannel = FileChannel.open(this, *options)
fun main(args: Array<String>) {
val buf: ByteBuffer = TODO()
val file = Paths.get("output")
file.channel(StandardOpenOption.CREATE, StandardOpenOption.WRITE).use { channel ->
while (buf.hasRemaining()) channel.write(buf)
}
}
elect
09/19/2017, 5:49 PMwhile (buf.hasRemaining())
and not simply channel.write(buf)
?diesieben07
09/19/2017, 6:03 PMchannel.write
may write only a portion of the buffer as per the Javadocs.elect
09/19/2017, 6:05 PMdiesieben07
09/19/2017, 6:11 PMChannel
is a very generic interface. It might be a channel bound to a network socket, which only has a limited buffer. You can't just send out 500MB immediately, you have to wait for the network speed. So as long as the network buffer is full, it cannot write.elect
09/19/2017, 8:28 PMval buffer = FileChannel.open(path, StandardOpenOption.READ).use { channel ->
bufferBig(channel.size().i).also {
while (channel.read(it) > 0) Unit
}
}
read()
returns the number of bytes readdiesieben07
09/20/2017, 2:09 PMalso
here?