示例#1
0
        public long CountOfNotEmptyLines([InjectSource] ExtendedFileInfo context)
        {
            if (context == null)
            {
                throw new InjectSourceNullReferenceException(typeof(ExtendedFileInfo));
            }

            using (var stream = new StreamReader(context.OpenRead()))
            {
                var lines = 0;
                while (!stream.EndOfStream)
                {
                    var line = stream.ReadLine();

                    if (line == string.Empty)
                    {
                        continue;
                    }

                    lines += 1;
                }

                return(lines);
            }
        }
示例#2
0
        public byte[] GetFileBytes([InjectSource] ExtendedFileInfo file, long bytesCount = long.MaxValue, long offset = 0)
        {
            if (file == null)
            {
                throw new InjectSourceNullReferenceException(typeof(FileInfo));
            }

            using (var stream = file.OpenRead())
                using (var reader = new BinaryReader(stream))
                {
                    if (offset > 0)
                    {
                        stream.Seek(offset, SeekOrigin.Begin);
                    }

                    var toRead = bytesCount < stream.Length ? bytesCount : stream.Length;

                    var bytes = new byte[toRead];

                    for (var i = 0; i < toRead; ++i)
                    {
                        bytes[i] = reader.ReadByte();
                    }

                    return(bytes);
                }
        }
示例#3
0
        public string GetLinesContainingWord([InjectSource] ExtendedFileInfo file, string word)
        {
            if (file == null)
            {
                throw new InjectSourceNullReferenceException(typeof(ExtendedFileInfo));
            }

            using (var stream = new StreamReader(file.OpenRead()))
            {
                var lines = new List <string>();
                var line  = 1;
                while (!stream.EndOfStream)
                {
                    var strLine = stream.ReadLine();
                    if (strLine != null && strLine.Contains(word))
                    {
                        lines.Add(line.ToString());
                    }
                    line += 1;
                }

                var builder = new StringBuilder("(");

                for (int i = 0, j = lines.Count - 1; i < j; ++i)
                {
                    builder.Append(lines[i]);
                }

                builder.Append(lines[lines.Count]);
                builder.Append(')');

                return(builder.ToString());
            }
        }
示例#4
0
        public string Md5File([InjectSource] ExtendedFileInfo file)
        {
            if (file == null)
            {
                throw new InjectSourceNullReferenceException(typeof(ExtendedFileInfo));
            }

            using (var stream = file.OpenRead())
            {
                return(HashHelper.ComputeHash <MD5CryptoServiceProvider>(stream));
            }
        }
示例#5
0
        public bool HasContent([InjectSource] ExtendedFileInfo file, string pattern)
        {
            if (file == null)
            {
                throw new InjectSourceNullReferenceException(typeof(ExtendedFileInfo));
            }

            using (var stream = new StreamReader(file.OpenRead()))
            {
                var content = stream.ReadToEnd();
                return(Regex.IsMatch(content, pattern));
            }
        }
示例#6
0
        public string GetFileContent([InjectSource] ExtendedFileInfo extendedFileInfo)
        {
            if (!extendedFileInfo.Exists)
            {
                return(null);
            }

            using (var file = extendedFileInfo.OpenRead())
                using (var fileReader = new StreamReader(file))
                {
                    return(fileReader.ReadToEnd());
                }
        }
示例#7
0
        public void OpenRead()
        {
            string tempFile = Path.GetTempFileName();

            ExtendedFileInfo efi = new ExtendedFileInfo(tempFile);

            Assert.IsNotNull(efi);

            using (FileStream stream = efi.OpenRead())
            {
                Assert.IsTrue(stream.CanRead);
                Assert.IsFalse(stream.CanWrite);
            }
        }
示例#8
0
        public byte[] Tail([InjectSource] ExtendedFileInfo file, int length)
        {
            if (file == null)
            {
                throw new InjectSourceNullReferenceException(typeof(FileInfo));
            }

            using (var stream = file.OpenRead())
                using (var reader = new BinaryReader(stream))
                {
                    var toRead = length < stream.Length ? length : stream.Length;

                    var bytes = new byte[toRead];

                    stream.Position = stream.Length - length;
                    for (var i = 0; i < toRead; ++i)
                    {
                        bytes[i] = reader.ReadByte();
                    }

                    return(bytes);
                }
        }