Skip to content

Commit

Permalink
Initially and partially implement VertxWriteStreamOutputStream to c…
Browse files Browse the repository at this point in the history
…onvert the `HttpServerResponse` to an `OutputStream` to be written to by kotlinx.serialization directly and give up

The `OutputStream` methods all deal with byte arrays, and it's still needed to convert byte arrays to buffers. I don't think this can improve the performance.
  • Loading branch information
ShreckYe committed Oct 28, 2024
1 parent 9add83e commit e6f4ef5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.html.*
import kotlinx.html.stream.appendHTML
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToStream
import java.net.SocketException
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
Expand Down Expand Up @@ -117,7 +119,8 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle() {
checkedCoroutineHandlerUnconfined {
it.response().run {
putJsonResponseHeader()
end(Json.encodeToString(serializer, requestHandler(it)))/*.coAwait()*/
@OptIn(ExperimentalSerializationApi::class)
Json.encodeToStream(serializer, requestHandler(it), toOutputStream())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import io.vertx.core.buffer.Buffer
import io.vertx.core.streams.WriteStream
import io.vertx.kotlin.coroutines.coAwait
import kotlinx.coroutines.runBlocking
import java.io.OutputStream

class VertxWriteStreamOutputStream(val writeStream: WriteStream<Buffer>) : OutputStream() {
override fun write(b: Int) {
runBlocking {
writeStream.write(Buffer.buffer(4).apply {
appendInt(b)
}).coAwait()
}
}

override fun write(b: ByteArray) {
TODO()
}

override fun write(b: ByteArray, off: Int, len: Int) {
TODO()
}

override fun close() {
TODO()
}

override fun flush() {
TODO()
}
}

fun WriteStream<Buffer>.toOutputStream() =
VertxWriteStreamOutputStream(this)

0 comments on commit e6f4ef5

Please sign in to comment.