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

ReadAsync_MemoryStream() static private méthode

static private ReadAsync_MemoryStream ( Stream stream, Windows.Storage.Streams.IBuffer buffer, UInt32 count ) : UInt32>.IAsyncOperationWithProgress
stream Stream
buffer Windows.Storage.Streams.IBuffer
count UInt32
Résultat UInt32>.IAsyncOperationWithProgress
        internal static IAsyncOperationWithProgress<IBuffer, UInt32> ReadAsync_MemoryStream(Stream stream, IBuffer buffer, UInt32 count)
        {
            Debug.Assert(stream != null);
            Debug.Assert(stream is SREMemoryStream);
            Debug.Assert(stream.CanRead);
            Debug.Assert(stream.CanSeek);
            Debug.Assert(buffer != null);
            Debug.Assert(buffer is IBufferByteAccess);
            Debug.Assert(0 <= count);
            Debug.Assert(count <= Int32.MaxValue);
            Debug.Assert(count <= buffer.Capacity);
            Contract.EndContractBlock();

            // We will return a different buffer to the user backed directly by the memory stream (avoids memory copy).
            // This is permitted by the WinRT stream contract.
            // The user specified buffer will not have any data put into it:
            buffer.Length = 0;

            SREMemoryStream memStream = stream as SREMemoryStream;
            Debug.Assert(memStream != null);

            try
            {
                IBuffer dataBuffer = memStream.GetWindowsRuntimeBuffer((Int32)memStream.Position, (Int32)count);
                if (dataBuffer.Length > 0)
                    memStream.Seek(dataBuffer.Length, SeekOrigin.Current);

                return AsyncInfo.CreateCompletedOperation<IBuffer, UInt32>(dataBuffer);
            }
            catch (Exception ex)
            {
                return AsyncInfo.CreateFaultedOperation<IBuffer, UInt32>(ex);
            }
        }  // ReadAsync_MemoryStream

Usage Example

Exemple #1
0
        public IAsyncOperationWithProgress <IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
        {
            if (buffer == null)
            {
                // Mapped to E_POINTER.
                throw new ArgumentNullException(nameof(buffer));
            }

            if (count < 0 || int.MaxValue < count)
            {
                ArgumentOutOfRangeException ex = new ArgumentOutOfRangeException(nameof(count));
                ex.HResult = __HResults.E_INVALIDARG;
                throw ex;
            }

            if (buffer.Capacity < count)
            {
                ArgumentException ex = new ArgumentException(SR.Argument_InsufficientBufferCapacity);
                ex.HResult = __HResults.E_INVALIDARG;
                throw ex;
            }

            if (!(options == InputStreamOptions.None || options == InputStreamOptions.Partial || options == InputStreamOptions.ReadAhead))
            {
                ArgumentOutOfRangeException ex = new ArgumentOutOfRangeException(nameof(options),
                                                                                 SR.ArgumentOutOfRange_InvalidInputStreamOptionsEnumValue);
                ex.HResult = __HResults.E_INVALIDARG;
                throw ex;
            }

            Stream str = EnsureNotDisposed();

            IAsyncOperationWithProgress <IBuffer, uint> readAsyncOperation;

            switch (_readOptimization)
            {
            case StreamReadOperationOptimization.MemoryStream:
                readAsyncOperation = StreamOperationsImplementation.ReadAsync_MemoryStream(str, buffer, count);
                break;

            case StreamReadOperationOptimization.AbstractStream:
                readAsyncOperation = StreamOperationsImplementation.ReadAsync_AbstractStream(str, buffer, count, options);
                break;

            // Use this pattern to add more optimisation options if necessary:
            //case StreamReadOperationOptimization.XxxxStream:
            //    readAsyncOperation = StreamOperationsImplementation.ReadAsync_XxxxStream(str, buffer, count, options);
            //    break;

            default:
                Debug.Fail("We should never get here. Someone forgot to handle an input stream optimisation option.");
                readAsyncOperation = null;
                break;
            }

            return(readAsyncOperation);
        }
All Usage Examples Of System.IO.StreamOperationsImplementation::ReadAsync_MemoryStream