/// <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. IFileAsync file = await Program.DavClient.OpenFileAsync(RemoteStorageUri); using (Stream stream = await file.GetReadStreamAsync(offset, fileSize)) { 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; } } } }
/// <inheritdoc/> 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); IFileAsync file = await Program.DavClient.OpenFileAsync(RemoteStoragePath); using (Stream stream = await file.GetReadStreamAsync(offset, length)) { const int bufferSize = 0x500000; // 5Mb. Buffer size must be multiple of 4096 bytes for optimal performance. await stream.CopyToAsync(output, bufferSize, length); } }
/// <summary> /// Reads file content from the remote storage. /// </summary> /// <param name="offset">Offset in bytes in file content to start reading from.</param> /// <param name="length">Lenth in bytes of the file content to read.</param> /// <returns>File content that corresponds to the provided offset and length.</returns> public async Task <byte[]> ReadAsync(long offset, long length) { // This method has a 60 sec timeout. // To process longer requests modify the IFolder.TransferDataAsync() implementation. IFileAsync file = await Program.DavClient.OpenFileAsync(RemoteStorageUri); using (Stream stream = await file.GetReadStreamAsync(offset, length)) { byte[] buffer = new byte[length]; int bufferPos = 0; int bytesRead = 0; while ((bytesRead = await stream.ReadAsync(buffer, bufferPos, (int)length)) > 0) { bufferPos += bytesRead; length -= bytesRead; } return(buffer); } }