IFileReadAsync Method |
Namespace: ITHit.FileSystem
Task<IFileMetadata> ReadAsync( Stream output, long offset, long length, IFileMetadata metadata, 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 timeout. 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 ReturnDataAsync(Byte, Int64, Int64) method.
The code below is part of 'VirtualFileSystem' C# sample provided with the SDK.
public async Task<IFileMetadata> ReadAsync(Stream output, long offset, long length, IFileMetadata metadata, ITransferDataOperationContext operationContext, ITransferDataResultContext resultContext, CancellationToken cancellationToken) { // On Windows this method has a 60 sec timeout. // To process longer requests and reset the timeout 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, metadata); 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, operationContext, metadata); } } // Return an updated item to the Engine. // In the returned data set the following fields: // - Content eTag. The Engine will store it to determine if the file content should be updated. // - Medatdata eTag. The Engine will store it to determine if the item metadata should be updated. return null; }