def guess_stream_needs_encoding(fileobj, default=True):
    """
    Guess the type (bytes/unicode) of this stream, and return whether or not it
    requires text to be encoded before written into it.
    """
    try:
        t = type(fileobj.read(0))
        if t is bytes:
            return True
        elif t is str:
            return False
    except Exception:
        pass
    try:
        mode = fileobj.mode
        if 'b' in mode:
            return True
        else:
            return False
    except Exception:
        pass
    return default
