What is a nice Kotlin way of copying data from a B...
# announcements
j
What is a nice Kotlin way of copying data from a ByteArrayOutputStream in chunks to an OutputStream? the non-chunked way seems to be
outputStream.use { out -> baos.writeTo(out) }
d
copyTo
already copies in chunks (it has a
bufferSize
parameter). Or what do you mean by "chunked"?
j
writeTo writes the whole thing when looking at the source:
Copy code
public synchronized void writeTo(OutputStream out) throws IOException {
        out.write(this.buf, 0, this.count);
    }
Kinda looking for something where I can yield inbetween each chunk to prevent a Blocking.IO coroutine from being hogged if somebody has a very slow internet connection outputStream.use { out -> baos.window(BufferSize) { chunk -> chunk.writeTo(out) yield() } }
d
Copy code
ByteArrayInputStream(baos.toByteArray()).use { 
        it.copyTo(output)
    }
That is chunkced, but yeah... not really in a way that lets you yield
j
that looks like a good place to start, thanks for this!
could make use of channels from here on
this works well
Copy code
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
		val yieldSize: Int = 1 * 1024 * 1024
		var bytesCopied = 0L
		var bytesAfterYield = 0L
		this.respondOutputStream {
			this.use { output ->
				ByteArrayInputStream(result.data?.toByteArray() ?: byteArrayOf()).use { input ->
					while (true) {
						val bytes = input.read(buffer).takeIf { it >= 0 } ?: break
						<http://LulaServer.log.info|LulaServer.log.info>("[${result.fileName}] writing: $bytes")
						output.write(buffer)
						if (bytesAfterYield >= yieldSize) {
							yield()
							bytesAfterYield %= yieldSize
						}
						bytesCopied += bytes
						bytesAfterYield += bytes
					}
				}
			}
		}