Only read the necessary amount of data from the input stream (fixes #39)