// PullBits ensures that at least nb bits exist in the bit buffer.
// If the underlying reader is a compress.BufferedReader, then this will fill
// the bit buffer with as many bits as possible, relying on Peek and Discard to
// properly advance the read offset. Otherwise, it will use ReadByte to fill the
// buffer with just the right number of bits.
PullBits ensures that at least nb bits exist in the bit buffer.
If the underlying reader is a compress.BufferedReader, then this will fill
the bit buffer with as many bits as possible, relying on Peek and Discard to
properly advance the read offset. Otherwise, it will use ReadByte to fill the
buffer with just the right number of bits.
// PullBits ensures that at least nb bits exist in the bit buffer . // If the underlying reader is a compress . BufferedReader then this will fill // the bit buffer with as many bits as possible relying on Peek and Discard to // properly advance the read offset . Otherwise it will use ReadByte to fill the // buffer with just the right number of bits .
func (pr *Reader) PullBits(nb uint) error {
	if pr.bufRd != nil {
		pr.discardBits += int(pr.fedBits - pr.numBits)
		for {
			if len(pr.bufPeek) == 0 {
				pr.fedBits = pr.numBits 
				if _, err := pr.Flush(); err != nil {
					return err
				}
				var err error
				cntPeek := 8 
				if pr.bufRd.Buffered() > cntPeek {
					cntPeek = pr.bufRd.Buffered()
				}
				pr.bufPeek, err = pr.bufRd.Peek(cntPeek)
				pr.bufPeek = pr.bufPeek[int(pr.numBits/8):] 
				if len(pr.bufPeek) == 0 {
					if pr.numBits >= nb {
						break
					}
					if err == io.EOF {
						err = io.ErrUnexpectedEOF
					}
					return err
				}
			}
			cnt := int(64-pr.numBits) / 8
			if cnt > len(pr.bufPeek) {
				cnt = len(pr.bufPeek)
			}
			for _, c := range pr.bufPeek[:cnt] {
				pr.bufBits |= uint64(pr.transform[c]) << pr.numBits
				pr.numBits += 8
			}
			pr.bufPeek = pr.bufPeek[cnt:]
			if pr.numBits > 56 {
				break
			}
		}
		pr.fedBits = pr.numBits
	} else {
		for pr.numBits < nb {
			c, err := pr.byteRd.ReadByte()
			if err != nil {
				if err == io.EOF {
					err = io.ErrUnexpectedEOF
				}
				return err
			}
			pr.bufBits |= uint64(pr.transform[c]) << pr.numBits
			pr.numBits += 8
			pr.Offset++
		}
	}
	return nil
}
func ( pr * Reader ) PullBits ( nb uint ) error { if pr . bufRd != nil { pr . discardBits += int ( pr . fedBits - pr . numBits ) for { if len ( pr . bufPeek ) == 0 { pr . fedBits = pr . numBits if _ , err := pr . Flush ( ) ; err != nil { return err } var err error cntPeek := 8 if pr . bufRd . Buffered ( ) > cntPeek { cntPeek = pr . bufRd . Buffered ( ) } pr . bufPeek , err = pr . bufRd . Peek ( cntPeek ) pr . bufPeek = pr . bufPeek [ int ( pr . numBits / 8 ) : ] if len ( pr . bufPeek ) == 0 { if pr . numBits >= nb { break } if err == io . EOF { err = io . ErrUnexpectedEOF } return err } } cnt := int ( 64 - pr . numBits ) / 8 if cnt > len ( pr . bufPeek ) { cnt = len ( pr . bufPeek ) } for _ , c := range pr . bufPeek [ : cnt ] { pr . bufBits |= uint64 ( pr . transform [ c ] ) << pr . numBits pr . numBits += 8 } pr . bufPeek = pr . bufPeek [ cnt : ] if pr . numBits > 56 { break } } pr . fedBits = pr . numBits } else { for pr . numBits < nb { c , err := pr . byteRd . ReadByte ( ) if err != nil { if err == io . EOF { err = io . ErrUnexpectedEOF } return err } pr . bufBits |= uint64 ( pr . transform [ c ] ) << pr . numBits pr . numBits += 8 pr . Offset ++ } } return nil }
PullBits ensures that at least nb bits exist in the bit buffer.
PullBits ensures that at least nb bits exist in the bit buffer .
// Don't discard bits just added // Minimum Peek amount to make progress // Skip buffered bits