/// <summary>
        /// Reads this file content from the remote storage.
        /// </summary>
        /// <param name="offset">File content offset, in bytes, to start reading from.</param>
        /// <param name="length">Data length to read, in bytes.</param>
        /// <param name="fileSize">Total file size, in bytes.</param>
        /// <param name="resultContext">
        /// You will use this parameter to return file content by
        /// calling <see cref="ITransferDataResultContext.ReturnData(byte[], long, long)"/>
        /// </param>
        public async Task ReadAsync(long offset, long length, long fileSize, ITransferDataResultContext resultContext)
        {
            // On Windows this method has a 60 sec timeout.
            // To process longer requests and reset the timout timer call the resultContext.ReportProgress() or resultContext.ReturnData() method.

            await using (FileStream stream = File.OpenRead(RemoteStoragePath))
            {
                const long MAX_CHUNK_SIZE = 0x500000; //5Mb

                long chunkSize = Math.Min(MAX_CHUNK_SIZE, length);

                stream.Seek(offset, SeekOrigin.Begin);

                long   total  = offset + length;
                byte[] buffer = new byte[chunkSize];
                long   bytesRead;
                while ((bytesRead = await stream.ReadAsync(buffer, 0, (int)chunkSize)) > 0)
                {
                    resultContext.ReturnData(buffer, offset, bytesRead);
                    offset   += bytesRead;
                    length   -= bytesRead;
                    chunkSize = Math.Min(MAX_CHUNK_SIZE, length);
                    if (offset >= total)
                    {
                        return;
                    }
                }
            }
        }
Example #2
0
        /// <inheritdoc/>
        public async Task TransferDataAsync(long offset, long length, ITransferDataOperationContext operationContext, ITransferDataResultContext resultContext)
        {
            // This method has a 60 sec timeout.
            // To process longer requests and reset the timout timer call IContextWindows.ReportProgress() method.

            LogMessage($"IFile.TransferDataAsync({offset}, {length})", this.FullPath);

            SimulateNetworkDelay(length, resultContext);


            string remoteStoragePath = Mapping.MapPath(this.FullPath);

            // Transfering file content.
            await using (FileStream stream = File.OpenRead(remoteStoragePath))
            {
                stream.Seek(offset, SeekOrigin.Begin);
                byte[] buffer    = new byte[length];
                int    bytesRead = await stream.ReadAsync(buffer, 0, (int)length);

                resultContext.ReturnData(buffer, offset, length);
            }
        }
Example #3
0
        /// <inheritdoc/>
        public async Task ReadAsync(Stream output, long offset, long length, ITransferDataOperationContext operationContext, ITransferDataResultContext resultContext)
        {
            // On Windows this method has a 60 sec timeout.
            // To process longer requests and reset the timout timer call the resultContext.ReportProgress() or resultContext.ReturnData() method.

            Logger.LogMessage($"{nameof(IFile)}.{nameof(ReadAsync)}({offset}, {length})", UserFileSystemPath);

            SimulateNetworkDelay(length, resultContext);

            await using (FileStream stream = System.IO.File.OpenRead(RemoteStoragePath))
            {
                stream.Seek(offset, SeekOrigin.Begin);
                const int bufferSize = 0x500000; // 5Mb. Buffer size must be multiple of 4096 bytes for optimal performance.
                await stream.CopyToAsync(output, bufferSize, length);
            }
        }
        /// <inheritdoc/>
        public async Task TransferDataAsync(long offset, long length, ITransferDataOperationContext operationContext, ITransferDataResultContext resultContext)
        {
            // On Windows this method has a 60 sec timeout.
            // To process longer requests and reset the timout timer call the resultContext.ReportProgress() or resultContext.ReturnData() method.

            // For files > 4Gb we have to use the OptionalLength.
            if (operationContext.FileSize > 0x100000000)
            {
                length += operationContext.OptionalLength;
            }

            Logger.LogMessage($"{nameof(IFile)}.{nameof(TransferDataAsync)}({offset}, {length})", UserFileSystemPath);

            SimulateNetworkDelay(length, resultContext);

            IVirtualFile userFile = await VirtualDrive.GetItemAsync <IVirtualFile>(UserFileSystemPath, Logger);

            await userFile.ReadAsync(offset, length, operationContext.FileSize, resultContext);
        }
Example #5
0
 public async Task TransferDataAsync(long offset, long length, ITransferDataOperationContext operationContext, ITransferDataResultContext resultContext)
 {
     throw new NotImplementedException();
 }
        //$>

        //$<IFile.TransferDataAsync
        /// <inheritdoc/>
        public async Task TransferDataAsync(long offset, long length, ITransferDataOperationContext operationContext, ITransferDataResultContext resultContext)
        {
            // This method has a 60 sec timeout.
            // To process longer requests and reset the timout timer call resultContext.ReportProgress() method.

            Logger.LogMessage($"IFile.TransferDataAsync({offset}, {length})", UserFileSystemPath);

            SimulateNetworkDelay(length, resultContext);

            long optionalLength = length + operationContext.OptionalLength;

            byte[] buffer = await new UserFile(UserFileSystemPath).ReadAsync(offset, optionalLength);

            resultContext.ReturnData(buffer, offset, optionalLength);
        }
        /// <inheritdoc/>
        public async Task ReadAsync(Stream output, long offset, long length, ITransferDataOperationContext operationContext, ITransferDataResultContext resultContext)
        {
            Logger.LogMessage($"{nameof(IFile)}.{nameof(ReadAsync)}({offset}, {length})", UserFileSystemPath);

            SimulateNetworkDelay(length, resultContext);

            await using (FileStream stream = System.IO.File.OpenRead(RemoteStoragePath))
            {
                stream.Seek(offset, SeekOrigin.Begin);
                const int bufferSize = 0x500000; // 5Mb. Buffer size must be multiple of 4096 bytes for optimal performance.
                await stream.CopyToAsync(output, bufferSize, length);
            }
        }
        public async Task TransferDataAsync(long offset, long length, ITransferDataOperationContext operationContext, ITransferDataResultContext resultContext)
        {
            Logger.LogMessage($"IFile.TransferDataAsync({offset}, {length})", UserFileSystemPath);

            byte[] buffer = new byte[length];

            await using (FileStream stream = File.OpenRead(RemoteStoragePath))
            {
                stream.Seek(offset, SeekOrigin.Begin);

                int bytesRead = await stream.ReadAsync(buffer, 0, (int)length);
            }

            resultContext.ReturnData(buffer, offset, length);
        }