def flatten(input, file=None, encoding=None, errors='strict'):
	"""Return a flattened representation of a cinje chunk stream.
	
	This has several modes of operation.  If no `file` argument is given, output will be returned as a string.
	The type of string will be determined by the presence of an `encoding`; if one is given the returned value is a
	binary string, otherwise the native unicode representation.  If a `file` is present, chunks will be written
	iteratively through repeated calls to `file.write()`, and the amount of data (characters or bytes) written
	returned.  The type of string written will be determined by `encoding`, just as the return value is when not
	writing to a file-like object.  The `errors` argument is passed through when encoding.
	
	We can highly recommend using the various stremaing IO containers available in the
	[`io`](https://docs.python.org/3/library/io.html) module, though
	[`tempfile`](https://docs.python.org/3/library/tempfile.html) classes are also quite useful.
	"""
	
	input = stream(input, encoding, errors)
	
	if file is None:  # Exit early if we're not writing to a file.
		return b''.join(input) if encoding else ''.join(input)
	
	counter = 0
	
	for chunk in input:
		file.write(chunk)
		counter += len(chunk)
	
	return counter