Fixed exception when reading near end of stream

Change-Id: I13e04af52d293cbb5aff78521a2c56e2771ed3e1