public int getBitsAvailableInCurrentByte() {
        int count = 0;
        for (int mask = cacheMask; mask != 0; mask >>>= 1) {
            ++count;
        }
        return count;
    }
Output stream writing to a byte array, and capable of writing 1 bit at a time, starting from the most significant bit.
src/main/java/org/apache/commons/imaging/formats/tiff/itu_t4/BitArrayOutputStream.java