Example #1
0
        public void ReadFile(ReadContent readContent, IProgress <TProggressType> progress)
        {
            FileInfo fileInfo = new FileInfo(_filename);

            if (fileInfo.Exists)
            {
                if (IsCompressed)
                {
                    using (ZipArchive archive = ZipFile.OpenRead(fileInfo.FullName))
                    {
                        if (archive.Entries.Count == 1)
                        {
                            using (Stream entryStream = archive.Entries[0].Open())
                            {
                                readContent(entryStream, progress);
                            }
                        }
                    }
                }
                else
                {
                    using (FileStream stream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read))
                    {
                        readContent(stream, progress);
                    }
                }
            }
        }
Example #2
0
 public void ReadFile(ReadContent readContent)
 {
     if (IsCompressedFile())
     {
         using (ZipArchive archive = ZipFile.OpenRead(_fileInfo.FullName))
         {
             foreach (ZipArchiveEntry entry in archive.Entries)
             {
                 string expectedEntry = GetZipfileEntryName();
                 if (entry.FullName.EndsWith(expectedEntry))
                 {
                     using (Stream entryStream = entry.Open())
                     {
                         readContent(entryStream);
                     }
                 }
             }
         }
     }
     else
     {
         using (FileStream stream = new FileStream(_fileInfo.FullName, FileMode.Open, FileAccess.Read))
         {
             readContent(stream);
         }
     }
 }
        public void WithRandomBinaryContent_ThenStillCanRead()
        {
            GivenRandomBytesContent();
            GivenLogFileExistWithContent();
            GivenInitialPosition(0);

            WhenLogReaderIsCreated();
            WhenAllDataIsRead();

            ReadContent.ShouldNotBeEmpty();
        }
        public void WithInitialPositionAtFileEnd_ThenNothingIsRead()
        {
            GivenRandomTextContent("\n");
            GivenUTF8WithBOMEncoding();
            GivenLogFileExistWithContent();
            GivenInitialPosition(RawContent.Length + Fixture.Create <int>());

            WhenLogReaderIsCreated();
            WhenAllDataIsRead();

            this.ShouldSatisfyAllConditions(
                () => Target.Position.ShouldBe(RawContent.Length),
                () => ReadContent.ShouldBeEmpty()
                );
        }
        public void WithNoContent_ThenNothingIsRead()
        {
            GivenNoContent();
            GivenUTF8NoBOMEcoding();
            GivenLogFileExistWithContent();
            GivenInitialPosition(0);

            WhenLogReaderIsCreated();
            WhenAllDataIsRead();

            this.ShouldSatisfyAllConditions(
                () => Target.Position.ShouldBe(0),
                () => ReadContent.ShouldBeEmpty()
                );
        }
        public void WithRandomTextContentNoBOM_ThenItCanBeRead(
            string lineBreak,
            int emptyLinesBetweenLines,
            int leadingEmptyLines,
            int trailingEmptyLines
            )
        {
            GivenRandomTextContent(lineBreak, emptyLinesBetweenLines, leadingEmptyLines, trailingEmptyLines);
            GivenUTF8NoBOMEcoding();
            GivenLogFileExistWithContent();
            GivenInitialPosition(0);

            WhenLogReaderIsCreated();
            WhenAllDataIsRead();

            Target.Position.ShouldBe(RawContent.Length);
            ReadContent.Length.ShouldBe(NormalisedContent.Length);
            ReadContent.Select(s => Encoding.UTF8.GetString(s.ToArray())).ShouldBe(NormalisedContent);
        }
        /// <summary>
        /// Parse response message and check for emptyness.
        /// </summary>
        /// <typeparam name="T">Type of response</typeparam>
        /// <param name="responseMessage">Response message which has been received</param>
        /// <param name="isEmpty">Check if content is empty</param>
        /// <param name="readContent">Method to read content</param>
        /// <returns>Typed response</returns>
        private static T ParseResponseMessage <T>(HttpResponseMessage responseMessage, IsEmptyCheck <T> isEmpty, ReadContent <T> readContent)
        {
            if (responseMessage.IsSuccessStatusCode)
            {
                Log.Information($"Status code of response is {responseMessage.StatusCode}");

                T content = readContent(responseMessage);

                if (!isEmpty(content))
                {
                    return(content);
                }

                Log.Error($"Response of {responseMessage.RequestMessage.RequestUri} is empty!");
                throw new EmptyHttpResponseException(responseMessage);
            }

            Log.Error($"Unexpected status code of response {responseMessage.StatusCode} when request {responseMessage.RequestMessage.RequestUri}!");
            throw new UnexpectedHttpStatusCodeExecption(responseMessage);
        }