简体   繁体   中英

C# - Why StreamReader ReadLine reading data before calling ReadLine?

I am using StreamReader over NetworkStream and I want just read one or more lines and another data is byte array (like file data) and I dont want to read that file data in StreamReader , for example I need to read text header line by line and when I see one empty line, header reads must end and next I must read bytes of file, When I test this with StreamReader I get problems because StreamReader read bytes before I calling ReadLine (after first ReadLine ) and after dispose StreamReader and using NetworkStream to read bytes I get block of bytes that is not start of file byte array after header, because StreamReader readed block of bytes in ReadLine and not called ReadLine .

What is wrong in StreamReader or my code settings?

using (var reader = new StreamReader(tcpClient.GetStream()))
{
    while (true)
    {
        var line = reader.ReadLine();
        headerResponse += line + "\r\n";
        if (line == "")
            break;
    }
    using (var fileStreamReader = tcpClient.GetStream())
    {
        byte[] bytes = new byte[1024];
        var readCount = fileStreamReader.Read(bytes, 0, bytes.Length);
    }
}

I created one CustomStreamReader and I read bytes one by one in ReadLine Method and fixed my problem:

public class CustomStreamReader : Stream
{
    NetworkStream CurrentStream { get; set; }

    public CustomStreamReader(NetworkStream currentStream)
    {
        CurrentStream = currentStream;
    }

    public override bool CanRead => CurrentStream.CanRead;

    public override bool CanSeek => CurrentStream.CanSeek;

    public override bool CanWrite => CurrentStream.CanWrite;

    public override long Length => CurrentStream.Length;

    public override long Position { get => CurrentStream.Position; set => CurrentStream.Position = value; }

    public override void Flush()
    {
        CurrentStream.Flush();
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return CurrentStream.Read(buffer, offset, count);
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return CurrentStream.Seek(offset, origin);
    }

    public override void SetLength(long value)
    {
        CurrentStream.SetLength(value);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        CurrentStream.Write(buffer, offset, count);
    }

    public string ReadLine()
    {
        List<byte> result = new List<byte>();
        do
        {
            int data = CurrentStream.ReadByte();
            if (data == -1)
                break;
            result.Add((byte)data);
            if (data == 13)
            {
                data = CurrentStream.ReadByte();
                if (data == -1)
                    break;
                result.Add((byte)data);
                if (data == 10)
                    break;
            }
        }
        while (true);
        return Encoding.UTF8.GetString(result.ToArray());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM