I'm looking into the source code for Okio in order to understand efficient byte transferring better, and as a toy example made a little ForwardingSource
which inverts individual bytes as they come along. For example, it transforms (unsigned) 0b1011
to (unsigned) 0b0100
.
class ByteInvertingSource(source: Source) : ForwardingSource(source) {
// temporarily stores incoming bytes
private val sourceBuffer: Buffer = Buffer()
override fun read(sink: Buffer, byteCount: Long): Long {
// read incoming bytes
val count = delegate.read(sourceBuffer, byteCount)
// write inverted bytes to sink
sink.write(
sourceBuffer.readByteArray().apply {
println("Converting: ${joinToString(",") { it.toString(2) }}")
forEachIndexed { index, byte -> this[index] = byte.inv() }
println("Converted : ${joinToString(",") { it.toString(2) }}")
}
)
return count
}
}
Is this optimal code?
Specifically:
- Do I really need the
sourceBuffer
field, or could I use another trick to transform the bytes directly? - Is it more efficient to read the individual bytes from
sourceBuffer
and write the individual bytes intosink
? (I can't find awrite(Byte)
method, so maybe that is a clue that it's not.)