/// <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; } } } }
//$> //$<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); }
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); }
/// <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); } }