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: 3.3.9602.0
Syntax
Task ReadAsync(
	Stream output,
	long offset,
	long length,
	ITransferDataOperationContext operationContext,
	ITransferDataResultContext resultContext
)

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.
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 and reporting operation progress and status.

Return Value

Type: Task
.
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.

Examples

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

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