(int numBits)
| 34 | } |
| 35 | |
| 36 | public int read(int numBits) throws IOException { |
| 37 | int res = 0; |
| 38 | while (numBits > 0 && !eof) { |
| 39 | if (remaining == 0) { |
| 40 | tmp = input.read(); |
| 41 | if (tmp == -1) { |
| 42 | eof = true; |
| 43 | return -1; |
| 44 | } |
| 45 | remaining = 8; |
| 46 | } |
| 47 | int toNibble = Math.min(remaining, numBits); |
| 48 | int toLeave = (remaining-toNibble); |
| 49 | int leaveMask = (1<<toLeave)-1; |
| 50 | |
| 51 | res = res << toNibble; |
| 52 | res += (tmp>>toLeave); |
| 53 | tmp = tmp & leaveMask; |
| 54 | |
| 55 | remaining -= toNibble; |
| 56 | numBits -= toNibble; |
| 57 | } |
| 58 | if (eof) return -1; |
| 59 | return res; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Counts the number of bits until the next zero (false) |
no outgoing calls