recommend refactoring this into a 'read stuff' method so you don't need to have the 'while ready, read stuff and pass it to the codec' twice.