private int nextNBits(int bitsToGet) {
	byte b, next, next2next;
	int l = data.length - 1;
        int bp = this.bytePointer;
	    
	if (fillOrder == 1) {
	    b = data[bp];
	    
	    if (bp == l) {
		next = 0x00;
		next2next = 0x00;
	    } else if ((bp + 1) == l) {
		next = data[bp + 1];
		next2next = 0x00;
	    } else {
		next = data[bp + 1];
		next2next = data[bp + 2];
	    }
	} else if (fillOrder == 2) {
	    b = flipTable[data[bp] & 0xff];

	    if (bp == l) {
		next = 0x00;
		next2next = 0x00;
	    } else if ((bp + 1) == l) {
		next = flipTable[data[bp + 1] & 0xff];
		next2next = 0x00;
	    } else {
		next = flipTable[data[bp + 1] & 0xff];
		next2next = flipTable[data[bp + 2] & 0xff];
	    }
	} else {
	    throw new RuntimeException(JaiI18N.getString("TIFFFaxDecoder7"));
	}

	int bitsLeft = 8 - bitPointer;
	int bitsFromNextByte = bitsToGet - bitsLeft;
	int bitsFromNext2NextByte = 0;
	if (bitsFromNextByte > 8) {
	    bitsFromNext2NextByte = bitsFromNextByte - 8;
	    bitsFromNextByte = 8;
	}
	
	bytePointer++;
	
	int i1 = (b & table1[bitsLeft]) << (bitsToGet - bitsLeft);
	int i2 = (next & table2[bitsFromNextByte]) >>> (8 - bitsFromNextByte);
	
	int i3 = 0;
	if (bitsFromNext2NextByte != 0) {
	    i2 <<= bitsFromNext2NextByte;
	    i3 = (next2next & table2[bitsFromNext2NextByte]) >>> 
		(8 - bitsFromNext2NextByte);
	    i2 |= i3;
	    bytePointer++;
	    bitPointer = bitsFromNext2NextByte;
	} else {
	    if (bitsFromNextByte == 8) {
		bitPointer = 0;
		bytePointer++;
	    } else {
		bitPointer = bitsFromNextByte;
	    }
	}
	
	int i = i1 | i2;
	return i;
    }