detect end of stream