| 26 | } |
| 27 | |
| 28 | public int getBits(int bitsToRead) { |
| 29 | if (bitsToRead == 0) |
| 30 | return 0; |
| 31 | if (eofFlag) |
| 32 | return -1; // Already at end of file |
| 33 | int toStore = 0; |
| 34 | while(bitsToRead != 0 && !eofFlag) { |
| 35 | if (bitsToRead >= 8 - currentBit) { |
| 36 | if (currentBit == 0) { // special |
| 37 | toStore = toStore << 8; |
| 38 | int cb = ((int) byteBuffer[currentByte]); |
| 39 | toStore += (cb<0 ? (int) 256 + cb : (int) cb); |
| 40 | bitsToRead -= 8; |
| 41 | currentByte++; |
| 42 | } else { |
| 43 | toStore = toStore << (8 - currentBit); |
| 44 | toStore += ((int) byteBuffer[currentByte]) & backMask[8 - currentBit]; |
| 45 | bitsToRead -= (8 - currentBit); |
| 46 | currentBit = 0; |
| 47 | currentByte++; |
| 48 | } |
| 49 | } else { |
| 50 | toStore = toStore << bitsToRead; |
| 51 | int cb = ((int) byteBuffer[currentByte]); |
| 52 | cb = (cb<0 ? (int) 256 + cb : (int) cb); |
| 53 | toStore += ((cb) & (0x00FF - frontMask[currentBit])) >> (8 - (currentBit + bitsToRead)); |
| 54 | currentBit += bitsToRead; |
| 55 | bitsToRead = 0; |
| 56 | } |
| 57 | if (currentByte == eofByte) { |
| 58 | eofFlag = true; |
| 59 | return toStore; |
| 60 | } |
| 61 | } |
| 62 | return toStore; |
| 63 | } |
| 64 | |
| 65 | } |