private int decode ( Huffman h ) throws IOException { int len ; /* current number of bits in code */ int code ; /* len bits being decoded */ int first ; /* first code of length len */ int count ; /* number of codes of length len */ int index ; /* index of first code of length len in symbol table */ int bitbuf ; /* bits from stream */ int left ; /* bits left in next or left to process */ //short *next; /* next number of codes */ bitbuf = m_bitbuf ; left = m_bitcnt ; code = first = index = 0 ; len = 1 ; int nextIndex = 1 ; // next = h->count + 1; while ( true ) { while ( left -- != 0 ) { code |= ( bitbuf & 1 ) ^ 1 ; /* invert code */ bitbuf >>= 1 ; //count = *next++; count = h . m_count [ nextIndex ++ ] ; if ( code < first + count ) { /* if length len, return symbol */ m_bitbuf = bitbuf ; m_bitcnt = ( m_bitcnt - len ) & 7 ; return h . m_symbol [ index + ( code - first ) ] ; } index += count ; /* else update for next length */ first += count ; first <<= 1 ; code <<= 1 ; len ++ ; } left = ( MAXBITS + 1 ) - len ; if ( left == 0 ) { break ; } if ( m_left == 0 ) { m_in = m_input . read ( ) ; m_left = m_in == - 1 ? 0 : 1 ; if ( m_left == 0 ) { throw new IOException ( "out of input" ) ; /* out of input */ } } bitbuf = m_in ; m_left -- ; if ( left > 8 ) { left = 8 ; } } return - 9 ; /* ran out of codes */ }
Decode a code from the stream s using huffman table h . Return the symbol or a negative value if there is an error . If all of the lengths are zero i . e . an empty code or if the code is incomplete and an invalid code is received then - 9 is returned after reading MAXBITS bits .