Пример #1
0
        public async Task <string> LoadFirstLinesFromDiskAsync(string fileName, int lineCount, SecureString passphrase)
        {
            using (var inputStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
            {
                using (var outputStream = new MemoryStream())
                {
                    var password = SecureStringCredentialStore.SecureStringToString(passphrase);
                    using (var cryptoStream = CipherStream.Open(inputStream, password))
                    {
                        while (cryptoStream.CanRead)
                        {
                            var buffer = new byte[4096];
                            await cryptoStream.ReadAsync(buffer, 0, 4096);

                            await outputStream.WriteAsync(buffer, 0, 4096);

                            char[] chunk      = Encoding.UTF8.GetChars(buffer);
                            var    occurances = chunk.Count(c => c == '\n');
                            if (occurances >= lineCount)
                            {
                                break;
                            }
                        }
                    }

                    outputStream.Position = 0;
                    using (var reader = new StreamReader(outputStream))
                    {
                        return(await reader.ReadToEndAsync());
                    }
                }
            }
        }