System.StreamExtensions.WriteNoAlloc C# (CSharp) Méthode

WriteNoAlloc() public static méthode

Writes byte count prefixed encoded text into the file. Byte count is written as 7-bit encoded 32-bit int. If no encoding is specified, UTF-8 will be used. Byte count prefix specifies number of bytes taken up by the string, not length of the string itself. Note that this method may allocate if the size of encoded string exceeds size of prepared buffer.
public static WriteNoAlloc ( this stream, string text, Encoding encoding = null ) : void
stream this
text string
encoding Encoding
Résultat void
        public static void WriteNoAlloc(this Stream stream, string text, Encoding encoding = null)
        {
            encoding = encoding ?? Encoding.UTF8;
            int byteCount = encoding.GetByteCount(text);
            stream.Write7BitEncodedInt(byteCount);

            var buffer = Buffer;
            if (byteCount > buffer.Length)
            {
                // mk:TODO: Make this work even when encoded string has to be written in parts (prepared buffer is not large enough).
                buffer = new byte[byteCount];
            }
            int bytesWritten = encoding.GetBytes(text, 0, text.Length, buffer, 0);
            Debug.Assert(bytesWritten == byteCount);
            stream.Write(buffer, 0, bytesWritten);
        }

Same methods

StreamExtensions::WriteNoAlloc ( this stream, Int16 v ) : void
StreamExtensions::WriteNoAlloc ( this stream, Int32 v ) : void
StreamExtensions::WriteNoAlloc ( this stream, System.Int64 v ) : void
StreamExtensions::WriteNoAlloc ( this stream, UInt16 v ) : void
StreamExtensions::WriteNoAlloc ( this stream, UInt32 v ) : void
StreamExtensions::WriteNoAlloc ( this stream, System.UInt64 v ) : void
StreamExtensions::WriteNoAlloc ( this stream, byte value ) : void
StreamExtensions::WriteNoAlloc ( this stream, byte bytes, int offset, int count ) : void
StreamExtensions::WriteNoAlloc ( this stream, decimal v ) : void
StreamExtensions::WriteNoAlloc ( this stream, double v ) : void
StreamExtensions::WriteNoAlloc ( this stream, float v ) : void