System.IO.MemoryStream.Write C# (CSharp) Method

Write() public method

public Write ( byte buffer, int offset, int count ) : void
buffer byte
offset int
count int
return void
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer), SR.ArgumentNull_Buffer);
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (buffer.Length - offset < count)
            {
                throw new ArgumentException(SR.Argument_InvalidOffLen);
            }

            if (!_isOpen)
            {
                throw new ObjectDisposedException(null, SR.ObjectDisposed_StreamClosed);
            }
            EnsureWriteable();

            int i = _position + count;
            // Check for overflow
            if (i < 0)
            {
                throw new IOException(SR.IO_IO_StreamTooLong);
            }

            if (i > _length)
            {
                bool mustZero = _position > _length;
                if (i > _capacity)
                {
                    bool allocatedNewArray = EnsureCapacity(i);
                    if (allocatedNewArray)
                    {
                        mustZero = false;
                    }
                }
                if (mustZero)
                {
                    Array.Clear(_buffer, _length, i - _length);
                }
                _length = i;
            }
            if ((count <= 8) && (buffer != _buffer))
            {
                int byteCount = count;
                while (--byteCount >= 0)
                {
                    _buffer[_position + byteCount] = buffer[offset + byteCount];
                }
            }
            else
            {
                Buffer.BlockCopy(buffer, offset, _buffer, _position, count);
            }
            _position = i;
        }

Usage Example

Beispiel #1
0
        public override void Write(ref NetworkStream netstream)
        {
            using (Stream stream = new MemoryStream())
            {
                // write id
                stream.Write(BitConverter.GetBytes((ushort)this.ID), 0, 2);

                // write body size
                stream.Write(BitConverter.GetBytes(0), 0, 4);

                // write isAdult
                stream.WriteByte(BitConverter.GetBytes(this.isAdult)[0]);

                // write UnderFifteen
                stream.WriteByte(BitConverter.GetBytes(this.UnderFifteen)[0]);

                // write Family
                stream.WriteByte(BitConverter.GetBytes(this.Family)[0]);

                // write Status
                stream.WriteByte(this.Status);

                // write LastDay
                stream.Write(BitConverter.GetBytes(this.LastDay), 0, 2);

                // write PayType
                stream.WriteByte(this.PayType);

                // copy stream to netstream
                stream.Position = 0;
                stream.CopyTo(netstream);
            }
        }
All Usage Examples Of System.IO.MemoryStream::Write