Handle stream close gracefully; don't search for xml declaration twice