System.IO.UnmanagedMemoryStream.Write C# (CSharp) Méthode

Write() private méthode

private Write ( byte buffer, int offset, int count ) : void
buffer byte
offset int
count int
Résultat 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);
            Contract.EndContractBlock();

            if (!_isOpen) throw new ObjectDisposedException(null, SR.ObjectDisposed_StreamClosed);
            if (!CanWrite) throw new NotSupportedException(SR.NotSupported_UnwritableStream);

            long pos = Interlocked.Read(ref _position);  // Use a local to avoid a race condition
            long len = Interlocked.Read(ref _length);
            long n = pos + count;
            // Check for overflow
            if (n < 0)
                throw new IOException(SR.IO_StreamTooLong);

            if (n > _capacity)
            {
                throw new NotSupportedException(SR.IO_FixedCapacity);
            }

            // Check to see whether we are now expanding the stream and must
            // zero any memory in the middle.
            if (pos > len)
            {
                unsafe
                {
                    Helpers.ZeroMemory(_mem + len, pos - len);
                }
            }

            // set length after zeroing memory to avoid race condition of accessing unzeroed memory
            if (n > len)
            {
                Interlocked.Exchange(ref _length, n);
            }

            unsafe
            {
                Marshal.Copy(buffer, offset, (IntPtr)(_mem + pos), count);
            }

            Interlocked.Exchange(ref _position, n);
            return;
        }

Usage Example

		public unsafe void Write(Stream value)
		{
			if (value.Length > _availableLength)
				throw new ArgumentException("Value is too long.");

			var buffer = new byte[4096];
			var lengthToWrite = value.Length;
			while (lengthToWrite > 0)
			{
				using (var stream = new UnmanagedMemoryStream(_currentPointer.Value.Ptr, _currentPointer.Value.AvailableLength, _currentPointer.Value.AvailableLength, FileAccess.ReadWrite))
				{
					do
					{
						var read = value.Read(buffer, 0, Math.Min(buffer.Length, _currentPointer.Value.AvailableLength));
						stream.Write(buffer, 0, read);

						lengthToWrite -= read;
						_currentPointer.Value.AvailableLength -= read;
					}
					while (_currentPointer.Value.AvailableLength > 0 && lengthToWrite > 0);
					
					_currentPointer = _currentPointer.Next;
				}
			}
		}
All Usage Examples Of System.IO.UnmanagedMemoryStream::Write