39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
namespace ln.protocols.helper
|
|
{
|
|
public static class StreamExtensions
|
|
{
|
|
public static string ReadLine(this Stream stream) => ReadLine(stream, Encoding.UTF8);
|
|
public static string ReadLine(this Stream stream, Encoding encoding)
|
|
{
|
|
if (!stream.CanRead)
|
|
return null;
|
|
|
|
byte[] line = new byte[1024];
|
|
int n = 0;
|
|
int ch;
|
|
while ((ch = stream.ReadByte()) != -1)
|
|
{
|
|
switch (ch)
|
|
{
|
|
case '\r':
|
|
break;
|
|
case '\n':
|
|
return encoding.GetString(line, 0, n);
|
|
default:
|
|
line[n++] = (byte)ch;
|
|
break;
|
|
}
|
|
}
|
|
if (n > 0)
|
|
return encoding.GetString(line, 0, n);
|
|
return null;
|
|
}
|
|
|
|
public static TextReader TextReader(this Stream stream) => new StreamReader(stream);
|
|
|
|
}
|
|
} |