module IO::Stream::Readable

def read_exactly(size, buffer = nil, exception: EOFError)

@returns [String] The data read from the stream.
@parameter exception [Class] The exception to raise if not enough data is available.
@parameter size [Integer] The number of bytes to read.
Read exactly the specified number of bytes.
def read_exactly(size, buffer = nil, exception: EOFError)
	if buffer = read(size, buffer)
		if buffer.bytesize != size
			raise exception, "Could not read enough data!"
		end
		
		return buffer
	end
	
	raise exception, "Stream finished before reading enough data!"
end