override def equals()

in util-core/src/main/scala/com/twitter/io/Buf.scala [565:607]


    override def equals(other: Any): Boolean = other match {
      case otherBuf: Buf if length == otherBuf.length =>
        otherBuf match {
          case Composite(otherBufs, _) =>
            // this is 2 nested loops, with the outer loop tracking which
            // Buf's they are on. The inner loop compares individual bytes across
            // the Bufs "segments".
            var otherBufIdx = 0
            var bufIdx = 0
            var byteIdx = 0
            var otherByteIdx = 0
            while (bufIdx < bufs.length && otherBufIdx < otherBufs.length) {
              val buf = bufs(bufIdx)
              val otherB = otherBufs(otherBufIdx)
              while (byteIdx < buf.length && otherByteIdx < otherB.length) {
                if (buf.get(byteIdx) != otherB.get(otherByteIdx))
                  return false
                byteIdx += 1
                otherByteIdx += 1
              }
              if (byteIdx == buf.length) {
                byteIdx = 0
                bufIdx += 1
              }
              if (otherByteIdx == otherB.length) {
                otherByteIdx = 0
                otherBufIdx += 1
              }
            }
            true

          case _ =>
            otherBuf.unsafeByteArrayBuf match {
              case Some(otherBab) =>
                equalsIndexed(otherBab)
              case None =>
                equalsIndexed(otherBuf)
            }
        }

      case _ =>
        false
    }