Fixes NPEs thrown when processing large binary messages.

Fixes NPEs thrown when processing large binary messages. Adds new test to show the problem.
I'm not sure that the patch is good. Maybe there is a better solution. But it fixes at least for me the NPEs.