private async Task <Document> LoadFileAsync(ILoader loader, string path, CancellationToken cancellationToken) { try { using (var file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: false)) using (var reader = new StreamReader(file)) { var count = 0; var buffer = new char[4096]; while ((count = await reader.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0) { cancellationToken.ThrowIfCancellationRequested(); if (!loader.AddData(buffer, count)) { throw new IOException("File can't be loaded!."); } } return(loader.ConvertToDocument()); } } catch { loader.Release(); throw; } }
private async Task <Document> LoadFileAsync(ILoader loader, string path, CancellationToken cancellationToken) { try { using (var file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)) using (var reader = new StreamReader(file)) { var count = 0; var buffer = new char[4096]; while ((count = await reader.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0) { // Check for cancellation cancellationToken.ThrowIfCancellationRequested(); // Add the data to the document if (!loader.AddData(buffer, count)) { throw new IOException("The data could not be added to the loader."); } } return(loader.ConvertToDocument()); } } catch { loader.Release(); throw; } }
private async Task<Document> LoadFileAsync(ILoader loader, string path, CancellationToken cancellationToken) { try { using (var file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)) using (var reader = new StreamReader(file)) { var count = 0; var buffer = new char[4096]; while ((count = await reader.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0) { // Check for cancellation cancellationToken.ThrowIfCancellationRequested(); // Add the data to the document if (!loader.AddData(buffer, count)) { throw new IOException("The data could not be added to the loader."); } } return loader.ConvertToDocument(); } } catch { loader.Release(); throw; } }
/// <summary> /// Loads a file using background document loader, background task (outside UI thread) /// </summary> /// <param name="loader">ILoader instance created with CreateLoader() method</param> /// <param name="path"></param> /// <param name="cancellationToken"></param> /// <param name="encoding"></param> /// <param name="detectBOM"></param> /// <returns></returns> private async Task<Document> LoadDocument(ILoader loader, string path, CancellationToken cancellationToken, Encoding encoding = null, bool detectBOM = true) { var buffer = new char[LoadingBufferSize]; var count = 0; try { using (var file = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, bufferSize: LoadingBufferSize, useAsync: true)) { if (encoding != null) Encoding = encoding; else if (Encoding == null) Encoding = Encoding.UTF8; using (var reader = new StreamReader(file, Encoding, detectBOM, LoadingBufferSize)) { while ((count = await reader.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0) { cancellationToken.ThrowIfCancellationRequested(); if (!loader.AddData(buffer, count)) throw new IOException("The data could not be added to the loader."); } return loader.ConvertToDocument(); } } } catch { loader.Release(); throw; } }