IFileReadAsync Method |
Namespace: ITHit.FileSystem
Task ReadAsync( Stream output, long offset, long length, ITransferDataOperationContext operationContext, ITransferDataResultContext resultContext, CancellationToken cancellationToken )
Called, when the file content should be transfetred from the remote torage to the user file system. For example when the file should be hydrated.
Writing to to the output stream provides maximum performance when done with 4KB-aligned blocks of data (except for the final block of the file). If not aligned blockes are written, the data will be automatically alligned to 4Kb.
On Windows platform this method has a 60 sec timout. To process longer requests report progress by calling the ReportProgress(Int64, Int64) method of the ITransferDataResultContext interface passed as a resultContext parameter. To reset the timeout you can also write the data to the output stream or call the ReturnData(Byte, Int64, Int64) method.
The code below is part of 'VirtualFileSystem' C# sample provided with the SDK.
public async Task ReadAsync(Stream output, long offset, long length, ITransferDataOperationContext operationContext, ITransferDataResultContext resultContext, CancellationToken cancellationToken) { // On Windows this method has a 60 sec timeout. // To process longer requests and reset the timout timer write to the output stream or call the resultContext.ReportProgress() or resultContext.ReturnData() methods. Logger.LogMessage($"{nameof(IFile)}.{nameof(ReadAsync)}({offset}, {length})", UserFileSystemPath, default, operationContext); if (!Mapping.TryGetRemoteStoragePathById(RemoteStorageItemId, out string remoteStoragePath)) return; using (FileStream stream = new FileInfo(remoteStoragePath).Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)) { stream.Seek(offset, SeekOrigin.Begin); const int bufferSize = 0x500000; // 5Mb. Buffer size must be multiple of 4096 bytes for optimal performance. try { await stream.CopyToAsync(output, bufferSize, length, cancellationToken); } catch (OperationCanceledException) { // Operation was canceled by the calling Engine.StopAsync() or the operation timeout occured. Logger.LogDebug($"{nameof(ReadAsync)}({offset}, {length}) canceled", UserFileSystemPath, default); } } }