public void TestStream() { var str = "abcdefghijklmnopqrstuvwxyz 中文"; using var stream = new MemoryStream(Encoding.UTF8.GetBytes(str)); var streamArr = stream.Separate(10).ToList(); Assert.IsTrue(streamArr.Count > 0); var reader = new CharsReader(streamArr, Encoding.UTF8); Assert.AreEqual(str, reader.ReadToEnd()); foreach (var s in streamArr) { s.Dispose(); } reader.Close(); str = "abcdefg\rhijklmn\nopq rst\r\nuvw xyz"; using var stream2 = CharsReader.ToStream(str); var lines = CharsReader.ReadLines(stream2, Encoding.UTF8).ToList(); Assert.AreEqual(4, lines.Count); reader = new CharsReader(str); Assert.AreEqual('a', reader.Peek()); Assert.AreEqual('a', reader.Read()); var arr = new char[] { ' ', ' ', ' ', ' ', ' ', ' ', ' ' }; reader.Read(arr, 1, 4); Assert.AreEqual('b', arr[1]); reader.ReadLine(); reader.Dispose(); }
/// <summary> /// Reads lines from a specific stream. /// </summary> /// <param name="file">The file to read.</param> /// <param name="detectEncodingFromByteOrderMarks">true if look for byte order marks at the beginning of the file; otherwise, false.</param> /// <param name="removeEmptyLine">true if need remove the empty line; otherwise, false.</param> /// <returns>Lines from the specific stream reader.</returns> /// <exception cref="ArgumentNullException">file was null.</exception> /// <exception cref="FileNotFoundException">file was not found.</exception> /// <exception cref="DirectoryNotFoundException">The directory of the file was not found.</exception> /// <exception cref="NotSupportedException">Cannot read the file.</exception> public static IEnumerable <string> ReadLines(FileInfo file, bool detectEncodingFromByteOrderMarks, bool removeEmptyLine = false) { if (file == null) { throw new ArgumentNullException(nameof(file), "file should not be null."); } using (var reader = new StreamReader(file.FullName, detectEncodingFromByteOrderMarks)) { return(CharsReader.ReadLines(reader, removeEmptyLine)); } }
/// <summary> /// Reads lines from a specific stream. /// </summary> /// <param name="file">The file to read.</param> /// <param name="detectEncodingFromByteOrderMarks">true if look for byte order marks at the beginning of the file; otherwise, false.</param> /// <param name="removeEmptyLine">true if need remove the empty line; otherwise, false.</param> /// <returns>Lines from the specific stream reader.</returns> /// <exception cref="ArgumentNullException">file was null.</exception> /// <exception cref="FileNotFoundException">file was not found.</exception> /// <exception cref="DirectoryNotFoundException">The directory of the file was not found.</exception> /// <exception cref="NotSupportedException">Cannot read the file.</exception> public static IEnumerable <string> ReadLines(this FileInfo file, bool detectEncodingFromByteOrderMarks, bool removeEmptyLine = false) { return(CharsReader.ReadLines(file, detectEncodingFromByteOrderMarks, removeEmptyLine)); }
/// <summary> /// Reads lines from a specific stream reader. /// </summary> /// <param name="reader">The stream reader.</param> /// <param name="removeEmptyLine">true if need remove the empty line; otherwise, false.</param> /// <returns>Lines from the specific stream reader.</returns> /// <exception cref="NotSupportedException">The stream does not support reading.</exception> /// <exception cref="IOException">An I/O error occurs.</exception> /// <exception cref="ObjectDisposedException">The stream has disposed.</exception> public static IEnumerable <string> ReadLines(this TextReader reader, bool removeEmptyLine = false) { return(CharsReader.ReadLines(reader, removeEmptyLine)); }
/// <summary> /// Reads lines from a specific stream. /// </summary> /// <param name="file">The file to read.</param> /// <param name="encoding">The character encoding to use.</param> /// <param name="removeEmptyLine">true if need remove the empty line; otherwise, false.</param> /// <returns>Lines from the specific stream reader.</returns> /// <exception cref="ArgumentNullException">file was null.</exception> /// <exception cref="FileNotFoundException">file was not found.</exception> /// <exception cref="DirectoryNotFoundException">The directory of the file was not found.</exception> /// <exception cref="NotSupportedException">Cannot read the file.</exception> /// <exception cref="IOException">An I/O error occurs.</exception> public static IEnumerable <string> ReadLines(this FileInfo file, Encoding encoding, bool removeEmptyLine = false) { return(CharsReader.ReadLines(file, encoding, removeEmptyLine)); }