def make_freq_table(stream):
    """
    Given an input stream, will construct a frequency table
    (i.e. mapping of each byte to the number of times it occurs in the stream).

    The frequency table is actually a dictionary.
    """
    freq_dict = {}
    bsize = 512                 # Use variable, to make FakeStream work
    buff = bytearray(bsize)
    end_of_stream = False
    while not end_of_stream:
        # read bytes into a pre-allocated bytes-like object (bytearray)
        # and return number of bytes read
        count = stream.readinto(buff)

        for single_byte in buff:
            if single_byte not in freq_dict:
                freq_dict[single_byte] = 1
            else:
                freq_dict[single_byte] += 1

        if count < bsize:       # end of stream
            end_of_stream = True

    return freq_dict