System.IO.StreamOperationsImplementation.WriteAsync_AbstractStream C# (CSharp) Méthode

WriteAsync_AbstractStream() static private méthode

static private WriteAsync_AbstractStream ( Stream stream, Windows.Storage.Streams.IBuffer buffer ) : UInt32>.IAsyncOperationWithProgress
stream Stream
buffer Windows.Storage.Streams.IBuffer
Résultat UInt32>.IAsyncOperationWithProgress
        internal static IAsyncOperationWithProgress<UInt32, UInt32> WriteAsync_AbstractStream(Stream stream, IBuffer buffer)
        {
            Debug.Assert(stream != null);
            Debug.Assert(stream.CanWrite);
            Debug.Assert(buffer != null);
            Contract.EndContractBlock();

            // Choose the optimal writing strategy for the kind of buffer supplied:
            Func<CancellationToken, IProgress<UInt32>, Task<UInt32>> writeOperation;
            Byte[] data;
            Int32 offset;

            // If buffer is backed by a managed array:
            if (buffer.TryGetUnderlyingData(out data, out offset))
            {
                writeOperation = async (cancelToken, progressListener) =>
                {
                    if (cancelToken.IsCancellationRequested)  // CancellationToken is non-nullable
                        return 0;

                    Debug.Assert(buffer.Length <= Int32.MaxValue);

                    Int32 bytesToWrite = (Int32)buffer.Length;

                    await stream.WriteAsync(data, offset, bytesToWrite, cancelToken).ConfigureAwait(continueOnCapturedContext: false);

                    if (progressListener != null)
                        progressListener.Report((UInt32)bytesToWrite);

                    return (UInt32)bytesToWrite;
                };
                // Otherwise buffer is of an unknown implementation:
            }
            else
            {
                writeOperation = async (cancelToken, progressListener) =>
                {
                    if (cancelToken.IsCancellationRequested)  // CancellationToken is non-nullable
                        return 0;

                    UInt32 bytesToWrite = buffer.Length;
                    Stream dataStream = buffer.AsStream();

                    Int32 buffSize = 0x4000;
                    if (bytesToWrite < buffSize)
                        buffSize = (Int32)bytesToWrite;

                    await dataStream.CopyToAsync(stream, buffSize, cancelToken).ConfigureAwait(continueOnCapturedContext: false);

                    if (progressListener != null)
                        progressListener.Report((UInt32)bytesToWrite);

                    return (UInt32)bytesToWrite;
                };
            }  // if-else

            // Construct and run the async operation:
            return AsyncInfo.Run<UInt32, UInt32>(writeOperation);
        }  // WriteAsync_AbstractStream

Usage Example

        public IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer)
        {
            if (buffer == null)
            {
                // Mapped to E_POINTER.
                throw new ArgumentNullException(nameof(buffer));
            }

            if (buffer.Capacity < buffer.Length)
            {
                ArgumentException ex = new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferLengthExceedsCapacity);
                ex.SetHResult(E_INVALIDARG);
                throw ex;
            }

            Stream str = EnsureNotDisposed();
            return StreamOperationsImplementation.WriteAsync_AbstractStream(str, buffer);
        }
All Usage Examples Of System.IO.StreamOperationsImplementation::WriteAsync_AbstractStream