Solve the sync reading from a stream problem by using a simple finite state machine.