12345678910111213141516171819202122232425262728293031323334 |
- using System.Text;
- namespace Bmp.Core.Common.Utility;
- public static class IoExtension
- {
- public static IEnumerable<string> ReadLines(this Stream stream, Encoding? encoding = null)
- {
- using var reader = encoding == null
- ? new StreamReader(stream, leaveOpen: true)
- : new StreamReader(stream, encoding, true);
- while (true)
- {
- var line = reader.ReadLine();
- if (line == null) yield break;
- yield return line;
- }
- }
- public static byte[] ReadBytes(this Stream stream, int count)
- {
- var buffer = new byte[count];
- var bytesRead = 0;
- while (bytesRead < buffer.Length)
- {
- var bytesToRead = buffer.Length - bytesRead;
- var n = stream.Read(buffer, bytesRead, bytesToRead);
- if (n == 0) break;
- bytesRead += n;
- }
- return buffer;
- }
- }
|