Click or drag to resize

IFileReadAsync Method

IT Hit User File System
Transfers data from remote sorage to the user file system.

Namespace:  ITHit.FileSystem
Assembly:  ITHit.FileSystem (in ITHit.FileSystem.dll) Version: 8.3.27213.0
Syntax
Task<IFileMetadata> ReadAsync(
	Stream output,
	long offset,
	long length,
	IFileMetadata metadata,
	ITransferDataOperationContext operationContext,
	ITransferDataResultContext resultContext,
	CancellationToken cancellationToken
)

Parameters

output
Type: System.IOStream
Stream to write the file content to.
offset
Type: SystemInt64
File content offset, in bytes, to start reading from.
length
Type: SystemInt64
Data length to read, in bytes.
metadata
Type: ITHit.FileSystemIFileMetadata
File metadata. Can be used to send eTag(s) or modification date to the remote storage to detect if file size or content changed and previously downloaded segments must be invalidated.
operationContext
Type: ITHit.FileSystemITransferDataOperationContext
Provides additional parameters for this operation and information about the environment.
resultContext
Type: ITHit.FileSystemITransferDataResultContext
Context for returning data to the platform, reporting operation progress and status.
cancellationToken
Type: System.ThreadingCancellationToken
The token to monitor for cancellation requests.

Return Value

Type: TaskIFileMetadata
Updated item. 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.
Remarks

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.

Examples

The code below is part of 'VirtualFileSystem' C# sample provided with the SDK.

C#
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;
}
See Also