Ok this is correct for traditional JPEG. Other flavors like Jpeg2000 use a similar (but lower overhead) version of this byte-stuffing to avoid JPEG markers from appearing in the compressed stream.
Related: https://en.wikipedia.org/wiki/JPEG#Syntax_and_structure