/// <summary> /// 断点文件下载 /// </summary> /// <param name="appfile"></param> /// <param name="path"></param> /// <param name="offset">断点开始位置</param> /// <param name="count"></param> /// <returns></returns> public Stream DownloadFileStream(string appfile, string path, long offset = 0, long count = 0) { if (!File.Exists(Utils.PathCombine(appfile, path))) { throw new FaultException("非法请求。"); } FileStream fstream = new FileStream(Utils.PathCombine(appfile, path), FileMode.Open, FileAccess.Read, FileShare.Read); if (offset >= fstream.Length) { throw new FaultException("请求的断点超过了文件最大长度。"); } if (count == 0) { count = fstream.Length - offset; } var outgoingResponse = WebOperationContext.Current.OutgoingResponse; outgoingResponse.ContentType = "application/force-download"; StreamReaderEx fs = new StreamReaderEx(fstream, offset, count); fs.Reading += (t) => { //限速代码,实际使用时可以去掉,或者精确控制 //Thread.Sleep(300); //Console.WriteLine(t); }; return(fstream); }
public static List <List <string> > ReadCsvFile(string fn, Encoding enc, int maxlines = -1) { List <List <string> > lines = new List <List <string> >(); int nline = 0; StreamReaderEx.OpenSteamForRead(fn, enc, sr => { while (!sr.EndOfStream && (maxlines <= 0 || nline < maxlines)) { nline++; var line = CsvFileHelper.GetLine(sr); bool hasNullVal = true; foreach (var fieldVal in line) { if (!string.IsNullOrEmpty(fieldVal)) { hasNullVal = false; break; } } if (!hasNullVal) { lines.Add(line); } } }); return(lines); }
public void TestReadLine_Performance() { using (var stream = File.OpenRead(TextLogSourceAcceptanceTest.File1Mb_1Line)) using (var reader = new StreamReaderEx(stream, Encoding.Default)) { reader.ExecutionTimeOf(x => x.ReadLine()).Should().BeLessThan(TimeSpan.FromSeconds(0.5)); } }
public void TestReadLine4() { _writer.Write("Foo\r\n"); _writer.Flush(); _stream.Position = 0; var reader = new StreamReaderEx(_stream, Encoding.UTF8); reader.ReadLine().Should().Be("Foo\r\n"); }
public void TestReadLine6() { var builder = new StringBuilder(); builder.Append('a', 2049); var line = builder.ToString(); _writer.Write(line); _writer.Flush(); _stream.Position = 0; var reader = new StreamReaderEx(_stream, Encoding.UTF8); reader.ReadLine().Should().Be(line); reader.ReadLine().Should().Be(null); }
public void TestReadBigLine1() { var fileName = TextLogSourceAcceptanceTest.File1Mb_1Line; var actualLines = File.ReadAllLines(fileName); using (var stream = File.OpenRead(fileName)) using (var reader = new StreamReaderEx(stream, Encoding.Default)) { var line1 = reader.ReadLine(); line1.Should().EndWith("\n"); var trimmedLine1 = line1.TrimEnd(); trimmedLine1.Equals(actualLines[0]).Should().BeTrue(); //< Using Should().Be(..) crashes VS because it's unable to print 2 Mb //line1.TrimEnd().Should().Be(actualLines[0]); reader.ReadLine().Should().BeNull("because there's no more lines"); } }
public void TestReadLine7() { var builder = new StringBuilder(); builder.Append('a', 2049); builder.Append("\r\n"); var line1 = builder.ToString(); _writer.Write(line1); _writer.Write("Foobar"); _writer.Flush(); _stream.Position = 0; var reader = new StreamReaderEx(_stream, Encoding.UTF8); reader.ReadLine().Should().Be(line1); reader.ReadLine().Should().Be("Foobar"); reader.ReadLine().Should().BeNull(); }
/// <inheritdoc /> protected override TimeSpan RunOnce(CancellationToken token) { bool read = false; try { if (!File.Exists(_fileName)) { SetDoesNotExist(); } else { var info = new FileInfo(_fileName); _properties.SetValue(LogFileProperties.LastModified, info.LastWriteTime); _properties.SetValue(LogFileProperties.Created, info.CreationTime); _properties.SetValue(LogFileProperties.Size, Size.FromBytes(info.Length)); using (var stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var format = _properties.GetValue(LogFileProperties.Format); if (format == null) { format = TryFindFormat(stream); _properties.SetValue(LogFileProperties.Format, format); } var encoding = format?.Encoding ?? _encoding; _properties.SetValue(LogFileProperties.Encoding, encoding); using (var reader = new StreamReaderEx(stream, encoding)) { // We change the error flag explicitly AFTER opening // the stream because that operation might throw if we're // not allowed to access the file (in which case a different // error must be set). _properties.SetValue(LogFileProperties.EmptyReason, ErrorFlags.None); if (stream.Length >= _lastPosition) { stream.Position = _lastPosition; } else { OnReset(stream, out _numberOfLinesRead, out _lastPosition); } string currentLine; while ((currentLine = reader.ReadLine()) != null) { token.ThrowIfCancellationRequested(); ResetEndOfSourceReached(); LevelFlags level = LogLine.DetermineLevelFromLine(currentLine); bool lastLineHadNewline = _lastLineHadNewline; var trimmedLine = currentLine.TrimNewlineEnd(out _lastLineHadNewline); var entryCount = _entries.Count; if (entryCount > 0 && !lastLineHadNewline) { // We need to remove the last line and create a new line // that consists of the entire content. RemoveLast(); trimmedLine = _untrimmedLastLine + trimmedLine; _untrimmedLastLine = _untrimmedLastLine + currentLine; } else { _untrimmedLastLine = currentLine; ++_numberOfLinesRead; read = true; } var timestamp = ParseTimestamp(trimmedLine); Add(trimmedLine, level, _numberOfLinesRead, timestamp); } _lastPosition = stream.Position; } } Listeners.OnRead(_numberOfLinesRead); SetEndOfSourceReached(); } } catch (FileNotFoundException e) { SetError(ErrorFlags.SourceDoesNotExist); Log.Debug(e); } catch (DirectoryNotFoundException e) { SetError(ErrorFlags.SourceDoesNotExist); Log.Debug(e); } catch (OperationCanceledException e) { Log.Debug(e); } catch (UnauthorizedAccessException e) { SetError(ErrorFlags.SourceCannotBeAccessed); Log.Debug(e); } catch (IOException e) { SetError(ErrorFlags.SourceCannotBeAccessed); Log.Debug(e); } catch (Exception e) { Log.Debug(e); } if (read) { return(TimeSpan.Zero); } return(TimeSpan.FromMilliseconds(100)); }
public void TestReadLine1() { var reader = new StreamReaderEx(_stream, Encoding.UTF8); reader.ReadLine().Should().BeNull("because the source stream is empty"); }
/// <summary> /// 下载文件 /// </summary> /// <param name="appfile"></param> /// <param name="path"></param> /// <returns></returns> public Stream DownloadFileStream(string appfile, string path) { if (!File.Exists(Utils.PathCombine(path, appfile))) { throw new FaultException("非法请求。"); } FileStream fstream = new FileStream(Utils.PathCombine(path, appfile), FileMode.Open, FileAccess.Read, FileShare.Read); var incomingRequest = WebOperationContext.Current.IncomingRequest; var outgoingResponse = WebOperationContext.Current.OutgoingResponse; long offset = 0, count = fstream.Length; if (incomingRequest.Headers.AllKeys.Contains("Range")) { var match = System.Text.RegularExpressions.Regex.Match(incomingRequest.Headers["Range"], @"(?<=bytes\b*=)(\d*)-(\d*)"); if (match.Success) { outgoingResponse.StatusCode = System.Net.HttpStatusCode.PartialContent; string v1 = match.Groups[1].Value; string v2 = match.Groups[2].Value; if (!match.NextMatch().Success) { if (v1 == "" && v2 != "") { var r2 = long.Parse(v2); offset = count - r2; count = r2; } else if (v1 != "" && v2 == "") { var r1 = long.Parse(v1); offset = r1; count -= r1; } else if (v1 != "" && v2 != "") { var r1 = long.Parse(v1); var r2 = long.Parse(v2); offset = r1; count -= r2 - r1 + 1; } else { outgoingResponse.StatusCode = System.Net.HttpStatusCode.OK; } } } outgoingResponse.ContentType = "application/force-download"; outgoingResponse.ContentLength = count; StreamReaderEx fs = new StreamReaderEx(fstream, offset, count); fs.Reading += (t) => { //限速代码,实际使用时可以去掉,或者精确控制 //Thread.Sleep(300); //Console.WriteLine(); }; } return(fstream); }