public void writeBits(long value, int n) throws IOException
    {
		// Note: As this method is called quite frequently, I'm
		// optimizing for speed instead of code readability. Consult
		// your local C, Assembler or other bit fiddling wizard if you
		// have trouble understanding it.
	
		value = (value & (-1L >>> (64-n))) | (((long)buffer) << n);
		n += bitsLeft;
		int remaining = n%8;
		int bytes = n/8;
		switch (bytes) {
		case 8:
		    bufferTmp[0] = (byte)(value >> (56+remaining));
		case 7:
		    bufferTmp[1] = (byte)(value >> (48+remaining));
		case 6:
		    bufferTmp[2] = (byte)(value >> (40+remaining));
		case 5:
		    bufferTmp[3] = (byte)(value >> (32+remaining));
		case 4:
		    bufferTmp[4] = (byte)(value >> (24+remaining));
		case 3:
		    bufferTmp[5] = (byte)(value >> (16+remaining));
		case 2:
		    bufferTmp[6] = (byte)(value >> (8+remaining));
		case 1:
		    bufferTmp[7] = (byte)(value >> remaining);
		    out.write(bufferTmp, 8-bytes, bytes);
		case 0:
		    bitsLeft = remaining;
		    // Store the remaining bits. Don't bother with masking out
		    // the 8-remaining bits in the low order byte of value -
		    // we're simply ignoring them the next time around...
		    buffer = (byte)value;
		    break;
		default:
		    throw new IndexOutOfBoundsException(Integer.toString(n));
		}
    }