I am currently struggling with `InputStreamAsInput...
# ktor
a
I am currently struggling with
InputStreamAsInput
. Current implementation seems to be bugged: 1) if I work with dynamic stream like the one I get from socket, it won't work since if input is empty, the input terminates. The input could be empty now, but not empty in the next moment. As a result I can't reliably convert socket input into
Input
2) It uses internal
ByteArray
pool and external
IOBuffer
pool, but sizes of those buffers are not synchronized. If someone would use non-default
IOBuffer
, It will probably break things.
🙏 1
👀 1
e
@cy
a
For the context, I am using
Input/Output
API internally in the framework and now trying to establish direct communication via sockets (I can use Ktor for that, but I do not want to bring the whole framework for that). For last two days I was struggling to understand why I lose last few bytes of the message and now I understand that stream to Input conversion is the culprit. Maybe there is a better way to do it (something with channels maybe?). I am currently trying to change implementation of InputStreamAsInput so it would work properly. I will post solution if I find one.
This simple code seems to do the thing:
Copy code
override fun fill(): IoBuffer? {

        val packet = stream.readPacketAtMost(4096)
        return pool.borrow().apply {
            resetForWrite(4096)
            writePacket(packet)
        }
    }
Though for some reason I randomly get 8-bytes buffers from
borrow
how is it possible if the pool is for 4096 bytes buffers?
It seems that there is no check of released buffers in the default IOBuffer pool implementation and someone is returning buffers of the wrong size. When I turn of the pooling, everything starts to work correctly.
I think you should really pay some attention to that part. We would like to contribute some fixes to kotlinx-io, but you should bring the master up to date with released code for that.