private void writeByte(final int b) {
        if (bytesWritten >= buffer.length) {
            final byte[] bigger = Allocator.byteArray(buffer.length * 2);
            System.arraycopy(buffer, 0, bigger, 0, bytesWritten);
            buffer = bigger;
        }
        buffer[bytesWritten++] = (byte) b;
    }
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