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); } }
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); } }
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()); } }
public string Md5File([InjectSource] ExtendedFileInfo file) { if (file == null) { throw new InjectSourceNullReferenceException(typeof(ExtendedFileInfo)); } using (var stream = file.OpenRead()) { return(HashHelper.ComputeHash <MD5CryptoServiceProvider>(stream)); } }
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)); } }
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()); } }
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); } }
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); } }