import typing


def _read_exact(stream: typing.BinaryIO, byte_count: int) -> bytes:
	"""Read ``byte_count`` bytes from ``stream`` and raise an exception if too few bytes are read
	(i. e. if EOF was hit prematurely).
	"""
	
	data = stream.read(byte_count)
	if len(data) != byte_count:
		raise ValueError(f"Attempted to read {byte_count} bytes of data, but only got {len(data)} bytes")
	return data