diff --git a/testtool/client/src/commonMain/kotlin/TesttoolClient.kt b/testtool/client/src/commonMain/kotlin/TesttoolClient.kt index 2d0aed85..156806d8 100644 --- a/testtool/client/src/commonMain/kotlin/TesttoolClient.kt +++ b/testtool/client/src/commonMain/kotlin/TesttoolClient.kt @@ -8,7 +8,6 @@ import io.ktor.client.* import io.ktor.client.call.* import io.ktor.client.plugins.* import io.ktor.client.request.* -import io.ktor.client.statement.* import io.ktor.http.content.* import kotlinx.coroutines.flow.* import kotlinx.io.* @@ -44,21 +43,16 @@ private val client = HttpClient { internal suspend fun postData(path: String, bytes: ByteArray): String = client.post(path) { setBody(ByteArrayContent(bytes)) -}.bodyAsText() +}.body().use { it.readString() } internal fun getData(path: String): Flow> = flow { - val source = client.get(path).body() - try { + client.get(path).body().use { source -> while (true) { - val idLength = source.readInt() + val idLength = runCatching { source.readInt() }.getOrNull() ?: break val id = source.readString(idLength.toLong()) - val contentLength = source.readLong().toInt() + val contentLength = source.readInt() val content = source.readByteArray(contentLength) emit(id to content) } - } catch (cause: EOFException) { - // ignore - } finally { - source.close() } } diff --git a/testtool/server/src/main/kotlin/compatibility.kt b/testtool/server/src/main/kotlin/compatibility.kt index d2a2982b..25bbae16 100644 --- a/testtool/server/src/main/kotlin/compatibility.kt +++ b/testtool/server/src/main/kotlin/compatibility.kt @@ -58,9 +58,11 @@ internal fun Route.routes( } private suspend fun ApplicationCall.saveFile(path: Path, name: String) { - val bytes = request.receiveChannel().readRemaining().readByteArray() + val buffer = request.receiveChannel().readBuffer() - path.createDirectories().resolve(name).writeBytes(bytes, StandardOpenOption.CREATE_NEW) + path.createDirectories().resolve(name) + .outputStream(StandardOpenOption.CREATE_NEW) + .use(buffer::readTo) } private suspend fun ApplicationCall.getFiles(path: Path, get: Path.() -> Pair) { @@ -79,8 +81,9 @@ private suspend fun ApplicationCall.getFiles(path: Path, get: Path.() -> Pair