Skip to content

Commit 24c22e3

Browse files
committed
WIP
1 parent 82659b7 commit 24c22e3

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

testtool/client/src/commonMain/kotlin/TesttoolClient.kt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import io.ktor.client.*
88
import io.ktor.client.call.*
99
import io.ktor.client.plugins.*
1010
import io.ktor.client.request.*
11-
import io.ktor.client.statement.*
1211
import io.ktor.http.content.*
1312
import kotlinx.coroutines.flow.*
1413
import kotlinx.io.*
@@ -44,21 +43,16 @@ private val client = HttpClient {
4443

4544
internal suspend fun postData(path: String, bytes: ByteArray): String = client.post(path) {
4645
setBody(ByteArrayContent(bytes))
47-
}.bodyAsText()
46+
}.body<Source>().use { it.readString() }
4847

4948
internal fun getData(path: String): Flow<Pair<String, ByteArray>> = flow {
50-
val source = client.get(path).body<Source>()
51-
try {
49+
client.get(path).body<Source>().use { source ->
5250
while (true) {
53-
val idLength = source.readInt()
51+
val idLength = runCatching { source.readInt() }.getOrNull() ?: break
5452
val id = source.readString(idLength.toLong())
55-
val contentLength = source.readLong().toInt()
53+
val contentLength = source.readInt()
5654
val content = source.readByteArray(contentLength)
5755
emit(id to content)
5856
}
59-
} catch (cause: EOFException) {
60-
// ignore
61-
} finally {
62-
source.close()
6357
}
6458
}

testtool/server/src/main/kotlin/compatibility.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ internal fun Route.routes(
5858
}
5959

6060
private suspend fun ApplicationCall.saveFile(path: Path, name: String) {
61-
val bytes = request.receiveChannel().readRemaining().readByteArray()
61+
val buffer = request.receiveChannel().readBuffer()
6262

63-
path.createDirectories().resolve(name).writeBytes(bytes, StandardOpenOption.CREATE_NEW)
63+
path.createDirectories().resolve(name)
64+
.outputStream(StandardOpenOption.CREATE_NEW)
65+
.use(buffer::readTo)
6466
}
6567

6668
private suspend fun ApplicationCall.getFiles(path: Path, get: Path.() -> Pair<String, Path>) {
@@ -79,8 +81,9 @@ private suspend fun ApplicationCall.getFiles(path: Path, get: Path.() -> Pair<St
7981
val idBytes = id.encodeToByteArray()
8082
output.writeInt(idBytes.size)
8183
output.write(idBytes)
82-
output.writeLong(contentPath.fileSize())
83-
output.write(contentPath.inputStream(), contentPath.fileSize())
84+
output.writeInt(contentPath.fileSize().toInt())
85+
contentPath.inputStream()
86+
.use(output::transferFrom)
8487
}
8588

8689
respondSource(output)

0 commit comments

Comments
 (0)