ensure outputs are always written in binary mode

This patch contains the code changes and test changes necessary for this.