read bits into an unsigned integer.
@param bits  The number of bits to read
@return      The bits as an unsigned integer
@throws IOException  Thrown if error reading input stream
428243
public int readRawUInt(int bits) throws IOException {
        int val = 0;
        for (int i = 0; i < bits; i++) {
            val = readBitToInt(val);
        }
        return val;
    }
863839
private int readBytes(byte[] b, int offs, int len)
		throws BitstreamException
	{
		int totalBytesRead = 0;
		try
		{
			while (len > 0)
			{
				int bytesread = source.read(b, offs, len);
				if (bytesread == -1)
				{
					break;
				}
				totalBytesRead += bytesread;
				offs += bytesread;
				len -= bytesread;
			}
		}
		catch (IOException ex)
		{
			throw newBitstreamException(STREAM_ERROR, ex);
		}
		return totalBytesRead;
	}
296350
@Override
  public int read(byte[] b, int off, int len) throws IOException {
    int bytes = inner.read(b, off, Math.min(len, remaining));
    if (bytes > 0) {
      remaining -= bytes;
    }
    return bytes;
  }
1056896
public Bits peek(final int bits) throws IOException {
    final int additionalBitsNeeded = bits - this.remainder.bitLength;
    final int additionalBytesNeeded = (int) Math.ceil(additionalBitsNeeded / 8.);
    if (additionalBytesNeeded > 0) this.readAhead(additionalBytesNeeded);
    return this.remainder.range(0, Math.min(bits, this.remainder.bitLength));
  }
868860
void readObject(int unsigned) throws IOException {
        handleObjectStart();
        byte b = (byte) unsigned;
        if (b >= MIN_FIX_INT) {  // no need to check for b <= MAX_FIX_INT
            handleInt(b);
            
        } else if (b >= MIN_FIX_STR && b <= MAX_FIX_STR) {
            string(b - MIN_FIX_STR);
            
        } else if (b >= MIN_FIX_ARRAY && b <= MAX_FIX_ARRAY) {
            array(b - MIN_FIX_ARRAY);
            
        } else if (b >= MIN_FIX_MAP && b <= MAX_FIX_MAP) {
            map(b - MIN_FIX_MAP);
            
        } else {
            switch ((int) b) {
                case NIL:
                    handleNil();
                    break;
                case FALSE:
                    handleBoolean(false);
                    break;
                case TRUE:
                    handleBoolean(true);
                    break;
                case BIN_8: {
                    binary(input.readUnsignedByte());
                    break;
                }
                case BIN_16: {
                    binary(input.readUnsignedShort());
                    break;
                }
                case BIN_32: {
                    binary(input.readInt());
                    break;
                }
                case EXT_8: {
                    extension(input.readUnsignedByte(), false);
                    break;
                }
                case EXT_16: {
                    extension(input.readUnsignedShort(), false);
                    break;
                }
                case EXT_32: {
                    extension(input.readInt(), false);
                    break;
                }
                case FLOAT_32: {
                    float value = input.readFloat();
                    handleFloat(value);
                    break;
                }
                case FLOAT_64: {
                    double value = input.readDouble();
                    handleDouble(value);
                    break;
                }
                case UINT_8: {
                    int value = input.readUnsignedByte();
                    handleInt(value);
                    break;
                }
                case UINT_16: {
                    int value = input.readUnsignedShort();
                    handleInt(value);
                    break;
                }
                case UINT_32: {
                    int val = input.readInt();
                    if (val >= 0) {
                        handleInt(val);
                    } else {
                        long value = ((long) val) & 0xFFFFFFFFL;
                        handleUnsignedLong(value);
                    }
                    break;
                }
                case UINT_64: {
                    long value = input.readLong();
                    handleUnsignedLong(value);
                    break;
                }
                case SINT_8: {
                    int value = input.readByte();
                    handleInt(value);
                    break;
                }
                case SINT_16: {
                    int value = input.readShort();
                    handleInt(value);
                    break;
                }
                case SINT_32: {
                    int value = input.readInt();
                    handleInt(value);
                    break;
                }
                case SINT_64: {
                    long value = input.readLong();
                    handleSignedLong(value);
                    break;
                }
                case FIX_EXT_1: {
                    extension(1, true);
                    break;
                }
                case FIX_EXT_2: {
                    extension(2, true);
                    break;
                }
                case FIX_EXT_4: {
                    extension(4, true);
                    break;
                }
                case FIX_EXT_8: {
                    extension(8, true);
                    break;
                }
                case FIX_EXT_16: {
                    extension(16, true);
                    break;
                }
                case STR_8: {
                    string(input.readUnsignedByte());
                    break;
                }
                case STR_16: {
                    string(input.readUnsignedShort());
                    break;
                }
                case STR_32: {
                    string(input.readInt());
                    break;
                }
                case ARRAY_16: {
                    array(input.readUnsignedShort());
                    break;
                }
                case ARRAY_32: {
                    array(input.readInt());
                    break;
                }
                case MAP_16: {
                    map(input.readUnsignedShort());
                    break;
                }
                case MAP_32: {
                    map(input.readInt());
                    break;
                }
                default:
                    handleUnknown(b);
            }
        }
    }
1124048
public T decode(final BitInputStream in) throws IOException {
    Bits remainder = in.readAhead(0);
    Entry<Bits, T> entry = this.forwardIndex.floorEntry(remainder);
    while (entry == null || !remainder.startsWith(entry.getKey())) {
      remainder = in.readAhead();
      entry = this.forwardIndex.floorEntry(remainder);
    }
    in.read(entry.getKey().bitLength);
    return entry.getValue();
  }
154424
public int read1Bit() throws IOException {
        if (nBit == 8) {
            advance();
            if (curByte == -1) {
                return -1;
            }
        }
        int res = (curByte >> (7 - nBit)) & 1;
        nBit++;

        debugBits.append(res == 0 ? '0' : '1');
        ++bitsRead;

        return res;
    }
1254869
protected int readInt(InputStream is) throws IOException
   {
      byte[] data = new byte[4];
      if (is.read(data) != data.length)
      {
         throw new EOFException();
      }

      return (MPPUtility.getInt(data, 0));
   }
1075452
def readbits(self, bits):
        
        
        if bits == 0:
            return 0
        
        # fast byte-aligned path
        if bits % 8 == 0 and self._bits_pending == 0:
            return self._read_bytes_aligned(bits // 8)
        
        out = 0
        masks = self._masks
        
        def transfer_bits(x, y, n, t):
            """
            transfers t bits from the top of y_n to the bottom of x.
            then returns x and the remaining bits in y
            """
            if n == t:
                # taking all
                return (x << t) | y, 0
            
            mask = masks[t]           # (1 << t) - 1
            remainmask = masks[n - t] # (1 << n - t) - 1
            taken = ((y >> n - t) & mask)
            return (x << t) | taken, y & remainmask
        
        while bits > 0:
            if self._bits_pending > 0:
                assert self._partial_byte is not None
                take = min(self._bits_pending, bits)
                out, self._partial_byte = transfer_bits(out, self._partial_byte, self._bits_pending, take)
                
                if take == self._bits_pending:
                    # we took them all
                    self._partial_byte = None
                self._bits_pending -= take
                bits -= take
                continue
            
            r = self.f.read(1)
            if r == b'':
                raise EOFError
            self._partial_byte = ord(r)
            self._bits_pending = 8
        
        return out
303420
public static byte bitsToByte(String bits) {
        byte b = 0;
        for (int i = bits.length() - 1, j = 0; i >= 0; i--, j++) {
            char c = bits.charAt(i);
            if (c == '1') {
                b += (1 << j);
            }
        }
        return b;
    }
1068571
static void writeBits(WriteStream writer, long bits, int count) {
		for (int i = (count - 1) & ~7; i >= 0; i -= 8) {
			writer.writeByte((byte) (bits >>> i));
		}
	}
184771
def read_bits(self, num):
        
        if num > len(self._bits):
            needed = num - len(self._bits)
            num_bytes = int(math.ceil(needed / 8.0))
            read_bytes = self._stream.read(num_bytes)

            for bit in bytes_to_bits(read_bytes):
                self._bits.append(bit)

        res = []
        while len(res) < num and len(self._bits) > 0:
            res.append(self._bits.popleft())

        return res
1234393
public function readBitStream($bits)
    {
        $this->readByteIfNeeded();

        $result = '';
        for ($i = 0; $i < $bits; $i++) {
            $mask = pow(2, $this->currentBitPosition);
            $result = ((($this->currentByte & $mask) === $mask) ? '1' : '0').$result;

            $this->currentBitPosition++;
            if (($i + 1) < $bits) {
                $this->readByteIfNeeded();
            }
        }

        return $result;
    }
106438
public long readRawULong(int bits) throws IOException {
        long val = 0;
        for (int i = 0; i < bits; i++) {
            val = readBitToLong(val);
        }
        return val;
    }
743459
public static int readInt(DataInput in) throws IOException {
        byte len=in.readByte();
        if(len == 0)
            return 0;
        return makeInt(in, len);
    }
1299719
public byte[] getBitString(int len) throws IOException {
        if (len > available())
            throw new IOException("short read of bit string");

        if (len == 0) {
            throw new IOException("Invalid encoding: zero length bit string");
        }

        int numOfPadBits = buf[pos];
        if ((numOfPadBits < 0) || (numOfPadBits > 7)) {
            throw new IOException("Invalid number of padding bits");
        }
        // minus the first byte which indicates the number of padding bits
        byte[] retval = new byte[len - 1];
        System.arraycopy(buf, pos + 1, retval, 0, len - 1);
        if (numOfPadBits != 0) {
            // get rid of the padding bits
            retval[len - 2] &= (0xff << numOfPadBits);
        }
        skip(len);
        return retval;
    }
488357
private int readByteFromStream() throws IOException {
    int result = this.in.read();
    if (result >= 0 && this.msb0) {
      result = JBBPUtils.reverseBitsInByte((byte) result) & 0xFF;
    }
    return result;
  }