def copyStream(src, dest, length = None, bufferSize = 16384):
    """Copy from one stream to another, up to a specified length"""
    amtread = 0
    while amtread != length:
        if length is None:
            bsize = bufferSize
        else:
            bsize = min(bufferSize, length - amtread)
        buf = src.read(bsize)
        if not buf:
            break
        dest.write(buf)
        amtread += len(buf)
    return amtread