Exemplo n.º 1
0
        private async Task FindingAndReplacingScriptInsideMapContentInBytes()
        {
            await Task.Run(() =>
            {
                var scriptTagInBytes = Encoding.UTF8.GetBytes("c_scrpt");
                var numberOfCharactersInScriptTag = scriptTagInBytes.Length;

                var nextTagInBytes = Encoding.UTF8.GetBytes("c_lr");

                var indexArrayOfFirstTagInBytes = _map.ContentInBytes.Locate(scriptTagInBytes);
                var indexesArrayNextTagInBytes  = _map.ContentInBytes.Locate(nextTagInBytes);

                if (indexArrayOfFirstTagInBytes.Length == 0 || indexesArrayNextTagInBytes.Length == 0)
                {
                    return;
                }

                var indexOfFirstTagInBytes           = indexArrayOfFirstTagInBytes[0];
                var indexOfNextTagInBytes            = indexesArrayNextTagInBytes[0];
                var endOfTransmissionCharacterCount  = 1;
                var indexOfFirstSizeByte             = indexOfFirstTagInBytes + numberOfCharactersInScriptTag;
                var indexOfByteBeforeFirstSizeByte   = indexOfFirstSizeByte - 1;
                var indexOfLastByteOfScript          = indexOfNextTagInBytes - endOfTransmissionCharacterCount - 1;
                var indexOfByteAfterLastByteOfScript = indexOfLastByteOfScript + 1;


                var newScriptTextBytes    = Encoding.UTF8.GetBytes(_script.ScriptText);
                var newScriptTextInBase64 = Convert.ToBase64String(newScriptTextBytes);

                var newScriptInBase64SizePrefixedInBytes = newScriptTextInBase64.ToSizePrefixedByteArray();

                var byteArrayUntilByteBeforeFirstSizeByte = new byte[indexOfByteBeforeFirstSizeByte + 1];

                var bytesCountFromByteAfterLastByteOfScriptUntilTheEnd = _map.ContentInBytes.Length - indexOfByteAfterLastByteOfScript;
                var byteArrayFromByteAfterLastByteOfScriptUntilTheEnd  = new byte[bytesCountFromByteAfterLastByteOfScriptUntilTheEnd + 1];

                Array.Copy(_map.ContentInBytes, sourceIndex: 0, destinationArray: byteArrayUntilByteBeforeFirstSizeByte, destinationIndex: 0, length: indexOfByteBeforeFirstSizeByte + 1);
                Array.Copy(_map.ContentInBytes, sourceIndex: indexOfByteAfterLastByteOfScript, destinationArray: byteArrayFromByteAfterLastByteOfScriptUntilTheEnd, destinationIndex: 0, length: bytesCountFromByteAfterLastByteOfScriptUntilTheEnd);

                _map.ContentInBytes = ByteExtensions.CombineByteArrays(byteArrayUntilByteBeforeFirstSizeByte, newScriptInBase64SizePrefixedInBytes, byteArrayFromByteAfterLastByteOfScriptUntilTheEnd);
            });
        }