https://kotlinlang.org logo
#squarelibraries
Title
# squarelibraries
j

jw

03/09/2023, 4:41 PM
Source is what you want. You read from the underlying one into a Buffer and then process it before writing to your Buffer. Presumably you'd do a loop to find the characters and then issue writes of the bytes that comes before one to your caller
👍🏼 1
👍🏻 1
d

dave08

03/13/2023, 1:41 PM
This is what ChatGPT suggested:
Copy code
import okio.*

class FilterCharacterSource(private val delegate: Source, private val charToRemove: Char) : ForwardingSource(delegate) {

    override fun read(sink: Buffer, byteCount: Long): Long {
        // Read from the delegate source into a temporary buffer
        val tempBuffer = Buffer()
        val bytesRead = delegate.read(tempBuffer, byteCount)

        // Remove the character to be filtered from the temporary buffer
        for (i in 0 until bytesRead) {
            val c = tempBuffer.readUtf8CodePoint().toChar()
            if (c != charToRemove) {
                sink.writeUtf8CodePoint(c.toInt())
            }
        }

        return bytesRead
    }
}
Does this look right?
6 Views