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

WriteAsync() public method

public WriteAsync ( Byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
buffer Byte
offset int
count int
cancellationToken CancellationToken
return Task
        public override Task WriteAsync(Byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            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);
            }

            return WriteAsyncImpl(buffer, offset, count, cancellationToken);
        }

Usage Example

Example #1
0
        private async Task WriteSection(int offsetPointer, int sizePointer, uint offset, uint size, byte[] section)
        {
            byte[] data    = this.SafeGetBuffer();
            uint   oldSize = size;

            using (var ms = new IO.MemoryStream())
                using (var bw = new IO.BinaryWriter(ms))
                {
                    // Write up to offset pointer
                    await ms.WriteAsync(data, 0, offsetPointer);

                    // Write new section coordinates
                    bw.Write(offset);
                    bw.Write((uint)section.Length);

                    // Write up to beginning of section
                    await ms.WriteAsync(data, sizePointer + SizeOfPointer, (int)(offset - (sizePointer + SizeOfPointer)));

                    // Write new section
                    await ms.WriteAsync(section, 0, section.Length);

                    // Write stuff after old section
                    await ms.WriteAsync(data, (int)(offset + oldSize), (int)(data.Length - (offset + oldSize)));

                    this.buffer = ms.ToArray();
                }
        }
All Usage Examples Of System.IO.MemoryStream::WriteAsync