public void pad(int factor) throws IOException {
        int padding = factor - (int) (nrBits % factor);
        int excess = padding & 7;
        if (excess > 0) {
            this.write(0, excess);
            padding -= excess;
        }
        while (padding > 0) {
            this.write(0, 8);
            padding -= 8;
        }
        this.out.flush();
    }