def _open_stream(on, outfile, mode, binary=False):
    """Open a binary or encoded text stream."""
    if not binary:
        encoding = 'utf-8-sig' if mode == 'r' else 'utf-8'
        return on.open(outfile, mode, encoding=encoding)
    else:
        return on.open(outfile, mode + 'b')