hpack/src/main/java/com/twitter/hpack/HuffmanDecoder.java
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();
  }
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.
@param  buf the string literal to be decoded
@return the output stream for the compressed data
@throws IOException if an I/O error occurs. In particular,
an <code>IOException</code> may be thrown if the
output stream has been closed.