System.IO.BinaryWriter.Write C# (CSharp) Метод

Write() публичный Метод

public Write ( string value ) : void
value string
Результат void
        public virtual void Write(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            int len = _encoding.GetByteCount(value);
            Write7BitEncodedInt(len);

            if (_largeByteBuffer == null)
            {
                _largeByteBuffer = new byte[LargeByteBufferSize];
                _maxChars = LargeByteBufferSize / _encoding.GetMaxByteCount(1);
            }

            if (len <= LargeByteBufferSize)
            {
                _encoding.GetBytes(value, 0, value.Length, _largeByteBuffer, 0);
                OutStream.Write(_largeByteBuffer, 0, len);
            }
            else
            {
                // Aggressively try to not allocate memory in this loop for
                // runtime performance reasons.  Use an Encoder to write out 
                // the string correctly (handling surrogates crossing buffer
                // boundaries properly).  
                int charStart = 0;
                int numLeft = value.Length;
#if DEBUG
                int totalBytes = 0;
#endif
                while (numLeft > 0)
                {
                    // Figure out how many chars to process this round.
                    int charCount = (numLeft > _maxChars) ? _maxChars : numLeft;
                    int byteLen;
                    byteLen = _encoder.GetBytes(value.ToCharArray(), charStart, charCount, _largeByteBuffer, 0, charCount == numLeft);
#if DEBUG
                    totalBytes += byteLen;
                    Debug.Assert(totalBytes <= len && byteLen <= LargeByteBufferSize, "BinaryWriter::Write(String) - More bytes encoded than expected!");
#endif
                    OutStream.Write(_largeByteBuffer, 0, byteLen);
                    charStart += charCount;
                    numLeft -= charCount;
                }
#if DEBUG
                Debug.Assert(totalBytes == len, "BinaryWriter::Write(String) - Didn't write out all the bytes!");
#endif
            }
        }

Same methods

BinaryWriter::Write ( bool value ) : void
BinaryWriter::Write ( byte value ) : void
BinaryWriter::Write ( byte buffer, int index, int count ) : void
BinaryWriter::Write ( char ch ) : void
BinaryWriter::Write ( char chars, int index, int count ) : void
BinaryWriter::Write ( decimal value ) : void
BinaryWriter::Write ( double value ) : void
BinaryWriter::Write ( float value ) : void
BinaryWriter::Write ( int value ) : void
BinaryWriter::Write ( long value ) : void
BinaryWriter::Write ( sbyte value ) : void
BinaryWriter::Write ( short value ) : void
BinaryWriter::Write ( uint value ) : void
BinaryWriter::Write ( ulong value ) : void
BinaryWriter::Write ( ushort value ) : void

Usage Example

 // DSFチャンクの数字はリトルエンディアンバイトオーダー
 private void BwWriteLE4(BinaryWriter bw, uint v)
 {
     bw.Write((byte)(v & 0xff));
     bw.Write((byte)((v >> 8) & 0xff));
     bw.Write((byte)((v >> 16) & 0xff));
     bw.Write((byte)((v >> 24) & 0xff));
 }
All Usage Examples Of System.IO.BinaryWriter::Write