private bool EncodeDynamicHeader(Span <byte> buffer, int staticTableIndex, string name, string value, int headerLength, Encoding?valueEncoding, out int bytesWritten) { EncoderHeaderEntry?headerField = GetEntry(name, value); if (headerField != null) { // Already exists in dynamic table. Write index. int index = CalculateDynamicTableIndex(headerField.Index); return(HPackEncoder.EncodeIndexedHeaderField(index, buffer, out bytesWritten)); } else { // Doesn't exist in dynamic table. Add new entry to dynamic table. int index = ResolveDynamicTableIndex(staticTableIndex, name); bool success = index == -1 ? HPackEncoder.EncodeLiteralHeaderFieldIndexingNewName(name, value, valueEncoding, buffer, out bytesWritten) : HPackEncoder.EncodeLiteralHeaderFieldIndexing(index, value, valueEncoding, buffer, out bytesWritten); if (success) { uint headerSize = (uint)headerLength; EnsureCapacity(headerSize); AddHeaderEntry(name, value, headerSize); } return(success); } }