public byte [ ] decode ( byte [ ] buf ) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ; Node node = root ; int current = 0 ; int bits = 0 ; for ( int i = 0 ; i < buf . length ; i ++ ) { int b = buf [ i ] & 0xFF ; current = ( current << 8 ) | b ; bits += 8 ; while ( bits >= 8 ) { int c = ( current >>> ( bits - 8 ) ) & 0xFF ; node = node . children [ c ] ; bits -= node . bits ; if ( node . isTerminal ( ) ) { if ( node . symbol == HpackUtil . HUFFMAN_EOS ) { throw EOS_DECODED ; } baos . write ( node . symbol ) ; node = root ; } } } while ( bits > 0 ) { int c = ( current << ( 8 - bits ) ) & 0xFF ; node = node . children [ c ] ; if ( node . isTerminal ( ) && node . bits <= bits ) { bits -= node . bits ; baos . write ( node . symbol ) ; node = root ; } else { break ; } } // Section 5.2. String Literal Representation // Padding not corresponding to the most significant bits of the code // for the EOS symbol (0xFF) MUST be treated as a decoding error. int mask = ( 1 << bits ) - 1 ; if ( ( current & mask ) != mask ) { throw INVALID_PADDING ; } return baos . toByteArray ( ) ; }
Decompresses the given Huffman coded string literal .