Skip to content

Commit 0ac9e89

Browse files
committedOct 11, 2024·
Optimize testtool
1 parent 6ea5657 commit 0ac9e89

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed
 

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

+10-17
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
package dev.whyoleg.cryptography.testtool.client
66

77
import io.ktor.client.*
8+
import io.ktor.client.call.*
89
import io.ktor.client.plugins.*
910
import io.ktor.client.request.*
10-
import io.ktor.client.statement.*
1111
import io.ktor.http.content.*
12-
import io.ktor.utils.io.*
13-
import io.ktor.utils.io.core.*
1412
import kotlinx.coroutines.flow.*
1513
import kotlinx.io.*
1614

@@ -45,21 +43,16 @@ private val client = HttpClient {
4543

4644
internal suspend fun postData(path: String, bytes: ByteArray): String = client.post(path) {
4745
setBody(ByteArrayContent(bytes))
48-
}.bodyAsText()
46+
}.body<Source>().use { it.readString() }
4947

5048
internal fun getData(path: String): Flow<Pair<String, ByteArray>> = flow {
51-
val channel = client.get(path).bodyAsChannel()
52-
while (true) {
53-
val idLength = channel.readIntOrNull() ?: break
54-
val id = channel.readPacket(idLength).readString()
55-
val contentLength = channel.readInt()
56-
val content = channel.readPacket(contentLength).readByteArray()
57-
emit(id to content)
49+
client.get(path).body<Source>().use { source ->
50+
while (!source.exhausted()) {
51+
val idLength = source.readInt()
52+
val id = source.readString(idLength.toLong())
53+
val contentLength = source.readInt()
54+
val content = source.readByteArray(contentLength)
55+
emit(id to content)
56+
}
5857
}
5958
}
60-
61-
private suspend fun ByteReadChannel.readIntOrNull(): Int? {
62-
val packet = readRemaining(4)
63-
if (packet.remaining == 0L) return null
64-
return packet.readInt()
65-
}

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

+17-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package dev.whyoleg.cryptography.testtool.server
66

7+
import io.ktor.http.*
78
import io.ktor.server.application.*
89
import io.ktor.server.response.*
910
import io.ktor.server.routing.*
@@ -57,13 +58,19 @@ internal fun Route.routes(
5758
}
5859

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

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

65-
private suspend fun ApplicationCall.getFiles(path: Path, get: Path.() -> Pair<String, Path>) = respondBytesWriter {
66-
if (!path.exists()) return@respondBytesWriter
68+
private suspend fun ApplicationCall.getFiles(path: Path, get: Path.() -> Pair<String, Path>) {
69+
if (!path.exists()) {
70+
respond(HttpStatusCode.OK)
71+
}
72+
73+
val output = Buffer()
6774

6875
path.forEachDirectoryEntry { entry ->
6976
val (id, contentPath) = get(entry)
@@ -72,10 +79,11 @@ private suspend fun ApplicationCall.getFiles(path: Path, get: Path.() -> Pair<St
7279
return@forEachDirectoryEntry
7380
}
7481
val idBytes = id.encodeToByteArray()
75-
val contentBytes = contentPath.readBytes()
76-
writeInt(idBytes.size)
77-
writeFully(idBytes)
78-
writeInt(contentBytes.size)
79-
writeFully(contentBytes)
82+
output.writeInt(idBytes.size)
83+
output.write(idBytes)
84+
output.writeInt(contentPath.fileSize().toInt())
85+
contentPath.inputStream().use(output::transferFrom)
8086
}
87+
88+
respondSource(output)
8189
}

0 commit comments

Comments
 (0)
Please sign in to comment.