def read_write(sin, sout, l=None, chunk_size=32) -> int:
    """Reads l or all bytes from sin and writes to sout"""
    # number of bytes written
    res = 0
    barr = bytearray(chunk_size)
    while True:
        if l and l < chunk_size:
            r = sin.read(l)
            sout.write(r)
            return res + len(r)
        else:
            r = sin.readinto(barr)
            if r == 0:
                return res
            res += r
            if r == chunk_size:
                sout.write(barr)
            else:
                sout.write(barr[:r])
            if l:
                l -= r
    return res