private void ProcessConcreteCompletedOperation(IAsyncOperationWithProgress <IBuffer, uint> completedOperation, out long bytesCompleted)
        {
            IBuffer resultBuffer = completedOperation.GetResults();

            Debug.Assert(resultBuffer != null);

            WinRtIOHelper.EnsureResultsInUserBuffer(_userBuffer, resultBuffer);
            bytesCompleted = _userBuffer.Length;
        }
Exemplo n.º 2
0
        private static async Task <Stream> OpenStreamForWriteAsyncCore(this IStorageFolder rootDirectory, String relativePath,
                                                                       CreationCollisionOption creationCollisionOption)
        {
            Contract.Requires(rootDirectory != null);
            Contract.Requires(!String.IsNullOrWhiteSpace(relativePath));

            Contract.Requires(creationCollisionOption == CreationCollisionOption.FailIfExists ||
                              creationCollisionOption == CreationCollisionOption.GenerateUniqueName ||
                              creationCollisionOption == CreationCollisionOption.OpenIfExists ||
                              creationCollisionOption == CreationCollisionOption.ReplaceExisting,
                              "The specified creationCollisionOption has a value that is not a value we considered when devising the"
                              + " policy about Append-On-OpenIfExists used in this method. Apparently a new enum value was added to the"
                              + " CreationCollisionOption type and we need to make sure that the policy still makes sense.");

            Contract.Ensures(Contract.Result <Task <Stream> >() != null);
            Contract.EndContractBlock();

            try
            {
                // Open file and set up default options for opening it:

                IStorageFile windowsRuntimeFile = await rootDirectory.CreateFileAsync(relativePath, creationCollisionOption)
                                                  .AsTask().ConfigureAwait(continueOnCapturedContext: false);

                Int64 offset = 0;

                // If the specified creationCollisionOption was OpenIfExists, then we will try to APPEND, otherwise we will OVERWRITE:

                if (creationCollisionOption == CreationCollisionOption.OpenIfExists)
                {
                    BasicProperties fileProperties = await windowsRuntimeFile.GetBasicPropertiesAsync()
                                                     .AsTask().ConfigureAwait(continueOnCapturedContext: false);

                    UInt64 fileSize = fileProperties.Size;

                    Debug.Assert(fileSize <= Int64.MaxValue, ".NET streams assume that file sizes are not larger than Int64.MaxValue,"
                                 + " so we are not supporting the situation where this is not the case.");
                    offset = checked ((Int64)fileSize);
                }

                // Now open a file with the correct options:

                Stream managedStream = await OpenStreamForWriteAsyncCore(windowsRuntimeFile, offset).ConfigureAwait(continueOnCapturedContext: false);

                return(managedStream);
            }
            catch (Exception ex)
            {
                // From this API, managed dev expect IO exceptions for "something wrong":
                WinRtIOHelper.NativeExceptionToIOExceptionInfo(ex).Throw();
                return(null);
            }
        }
        // Moved it to the end while using Dev10 VS because it does not understand async and everything that follows looses intellisense.
        // Should move this code into the Reading regios once using Dev11 VS becomes the norm.

        private async Task <Int32> ReadAsyncInternal(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
        {
            Debug.Assert(buffer != null);
            Debug.Assert(offset >= 0);
            Debug.Assert(count >= 0);
            Debug.Assert(buffer.Length - offset >= count);
            Debug.Assert(_canRead);

            IInputStream wrtStr = EnsureNotDisposed <IInputStream>();

#if DEBUG
            AssertValidStream(wrtStr);
#endif  // DEBUG

            try
            {
                IBuffer userBuffer = buffer.AsBuffer(offset, count);
                IAsyncOperationWithProgress <IBuffer, UInt32> asyncReadOperation = wrtStr.ReadAsync(userBuffer,
                                                                                                    unchecked ((UInt32)count),
                                                                                                    InputStreamOptions.Partial);

                IBuffer resultBuffer = await asyncReadOperation.AsTask(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);

                // If cancellationToken was cancelled until now, then we are currently propagating the corresponding cancellation exception.
                // (It will be correctly rethrown by the catch block below and overall we will return a cancelled task.)
                // But if the underlying operation managed to complete before it was cancelled, we want
                // the entire task to complete as well. This is ok as the continuation is very lightweight:

                if (resultBuffer == null)
                {
                    return(0);
                }

                WinRtIOHelper.EnsureResultsInUserBuffer(userBuffer, resultBuffer);

                Debug.Assert(resultBuffer.Length <= unchecked ((UInt32)Int32.MaxValue));
                return((Int32)resultBuffer.Length);
            }
            catch (Exception ex)
            {
                // If the interop layer gave us an Exception, we assume that it hit a general/unknown case and wrap it into
                // an IOException as this is what Stream users expect.
                WinRtIOHelper.NativeExceptionToIOExceptionInfo(ex).Throw();
                return(0);
            }
        }
Exemplo n.º 4
0
        private static async Task <Stream> OpenStreamForReadAsyncCore(this IStorageFile windowsRuntimeFile)
        {
            Debug.Assert(windowsRuntimeFile != null);

            try
            {
                IRandomAccessStream windowsRuntimeStream = await windowsRuntimeFile.OpenAsync(FileAccessMode.Read)
                                                           .AsTask().ConfigureAwait(continueOnCapturedContext: false);

                Stream managedStream = windowsRuntimeStream.AsStreamForRead();
                return(managedStream);
            }
            catch (Exception ex)
            {
                // From this API, managed dev expect IO exceptions for "something wrong":
                WinRtIOHelper.NativeExceptionToIOExceptionInfo(ex).Throw();
                return(null);
            }
        }
Exemplo n.º 5
0
        private static async Task <Stream> OpenStreamForReadAsyncCore(this IStorageFolder rootDirectory, string relativePath)
        {
            Debug.Assert(rootDirectory != null);
            Debug.Assert(!string.IsNullOrWhiteSpace(relativePath));

            try
            {
                IStorageFile windowsRuntimeFile = await rootDirectory.GetFileAsync(relativePath)
                                                  .AsTask().ConfigureAwait(continueOnCapturedContext: false);

                Stream managedStream = await windowsRuntimeFile.OpenStreamForReadAsync()
                                       .ConfigureAwait(continueOnCapturedContext: false);

                return(managedStream);
            }
            catch (Exception ex)
            {
                // From this API, managed dev expect IO exceptions for "something wrong":
                WinRtIOHelper.NativeExceptionToIOExceptionInfo(ex).Throw();
                return(null);
            }
        }
Exemplo n.º 6
0
        private static async Task <Stream> OpenStreamForWriteAsyncCore(this IStorageFile windowsRuntimeFile, Int64 offset)
        {
            Contract.Requires(windowsRuntimeFile != null);
            Contract.Requires(offset >= 0);
            Contract.Ensures(Contract.Result <Task <Stream> >() != null);
            Contract.EndContractBlock();

            try
            {
                IRandomAccessStream windowsRuntimeStream = await windowsRuntimeFile.OpenAsync(FileAccessMode.ReadWrite)
                                                           .AsTask().ConfigureAwait(continueOnCapturedContext: false);

                Stream managedStream = windowsRuntimeStream.AsStreamForWrite();
                managedStream.Position = offset;
                return(managedStream);
            }
            catch (Exception ex)
            {
                // From this API, managed dev expect IO exceptions for "something wrong":
                WinRtIOHelper.NativeExceptionToIOExceptionInfo(ex).Throw();
                return(null);
            }
        }
 private void ThrowWithIOExceptionDispatchInfo(Exception e)
 {
     WinRtIOHelper.NativeExceptionToIOExceptionInfo(ExceptionSupport.AttachRestrictedErrorInfo(_completedOperation.ErrorCode)).Throw();
 }