private byte[] writeHeaders(List<Header> headerBlock) throws IOException {
            if (mEmitDynamicTableSizeUpdate) {
                if (mSmallestHeaderTableSizeSetting < mMaxDynamicTableByteCount) {
                    // Multiple dynamic table size updates!
                    writeInt(mSmallestHeaderTableSizeSetting, PREFIX_5_BITS, 0x20);
                }
                mEmitDynamicTableSizeUpdate = false;
                mSmallestHeaderTableSizeSetting = Integer.MAX_VALUE;
                writeInt(mMaxDynamicTableByteCount, PREFIX_5_BITS, 0x20);
            }

            for (int i = 0, size = headerBlock.size(); i < size; i++) {
                Header header = headerBlock.get(i);
                String name = header.name.toLowerCase();
                String value = header.value;
                int headerIndex = -1;
                int headerNameIndex = -1;

                Integer staticIndex = NAME_TO_FIRST_INDEX.get(name);
                if (staticIndex != null) {
                    headerNameIndex = staticIndex + 1;
                    if (headerNameIndex > 1 && headerNameIndex < 8) {
                        // Only search a subset of the static header table. Most entries have an empty value, so
                        // it's unnecessary to waste cycles looking at them. This check is built on the
                        // observation that the header entries we care about are in adjacent pairs, and we
                        // always know the first index of the pair.
                        if (TextUtils.equals(STATIC_HEADER_TABLE[headerNameIndex - 1].value, value)) {
                            headerIndex = headerNameIndex;
                        } else if (TextUtils.equals(STATIC_HEADER_TABLE[headerNameIndex].value, value)) {
                            headerIndex = headerNameIndex + 1;
                        }
                    }
                }

                if (headerIndex == -1) {
                    for (int j = mNextHeaderIndex + 1, length = mDynamicTable.length; j < length; j++) {
                        if (TextUtils.equals(mDynamicTable[j].name, name)) {
                            if (TextUtils.equals(mDynamicTable[j].value, value)) {
                                headerIndex = j - mNextHeaderIndex + STATIC_HEADER_TABLE.length;
                                break;
                            } else if (headerNameIndex == -1) {
                                headerNameIndex = j - mNextHeaderIndex + STATIC_HEADER_TABLE.length;
                            }
                        }
                    }
                }

                if (headerIndex != -1) {
                    // Indexed Header Field.
                    writeInt(headerIndex, PREFIX_7_BITS, 0x80);
                } else if (headerNameIndex == -1) {
                    // Literal Header Field with Incremental Indexing - New Name.
                    mOut.write(0x40);
                    writeString(name);
                    writeString(value);
                    insertIntoDynamicTable(header);
                } else if (name.startsWith(Header.PSEUDO_PREFIX) && !Header.TARGET_AUTHORITY.equals(name)) {
                    // Follow Chromes lead - only include the :authority pseudo header, but exclude all other
                    // pseudo headers. Literal Header Field without Indexing - Indexed Name.
                    writeInt(headerNameIndex, PREFIX_4_BITS, 0);
                    writeString(value);
                } else {
                    // Literal Header Field with Incremental Indexing - Indexed Name.
                    writeInt(headerNameIndex, PREFIX_6_BITS, 0x40);
                    writeString(value);
                    insertIntoDynamicTable(header);
                }
            }
            return mOut.toByteArray();
        }
Read and write HPACK v10.

https://httpwg.org/specs/rfc7540.html#HeaderBlock

This implementation uses an array for the dynamic table and a list for indexed entries.  Dynamic
entries are added to the array, starting in the last position moving forward.  When the array
fills, it is doubled.

@author Megatron King
@since 2019/1/5 20:13
netbare-core/src/main/java/com/github/megatronking/netbare/http2/Hpack.java