title: Efficient mapping of byte buffers
body: <p>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 <code>ForwardingSource</code> which inverts individual bytes as they come along. For example, it transforms (unsigned) <code>0b1011</code> to (unsigned) <code>0b0100</code>.</p>
  <pre><code>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(&quot;Converting: ${joinToString(&quot;,&quot;) { it.toString(2) }}&quot;)
          forEachIndexed { index, byte -&gt; this[index] = byte.inv() }
          println(&quot;Converted : ${joinToString(&quot;,&quot;) { it.toString(2) }}&quot;)
        }
      )
      return count
    }
  }
  </code></pre>
  <p>Is this optimal code?</p>
  <p>Specifically:</p>
  <ul>
  <li>Do I really need the <code>sourceBuffer</code> field, or could I use another trick to transform the bytes directly?</li>
  <li>Is it more efficient to read the individual bytes from <code>sourceBuffer</code> and write the individual bytes into <code>sink</code>? (I can't find a <code>write(Byte)</code> method, so maybe that is a clue that it's not.)</li>
  </ul>