HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Replace EOL in stream

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
streamreplaceeol

Problem

I need to replace the windows EOL "\r\n" with just "\n", and I want to this in the fastest way possible, as the software will have a lot of files to upload, many of them with a few thousands lines. My current approach is:

UnicodeEncoding uniEncoding = new UnicodeEncoding();
using (MemoryStream newStream = new MemoryStream())
{
    using (TextReader stream = new StreamReader(fileInfo.Open(FileMode.Open, FileAccess.Read)))
    {
        string line = null;
        while ((line = await stream.ReadLineAsync()) != null)
        {
            line += "\n";
            await newStream.WriteAsync(uniEncoding.GetBytes(line), 0, line.Length);
        }
    }

    client.UploadFile(newStream, remote, true);
}


Another approach I was considering was to wrap the Stream I get from the file, and just not return the \n when read. I think this would consume less memory, but I am not sure if this would cause more problems as I have never really worked with streams.

Any feedback on my current approach / alternatives would be appreciated.

Solution

Performance

  • The non-Async methods are much faster in case of ReadLine/Write. If you need to run the code in background, use await Task.Run(/ put the whole code here /);



Encoding

  • You can use the static property Encoding.Unicode. (It is the same as creating a UnicodeEncoding instance without constructor arguments)



  • Add the Encoding to the stream reader: new StreamReader(fileInfo.Open(FileMode.Open, FileAccess.Read), Encoding.Unicode). Otherwise, if the input file is not unicode encoded, the output becomes a bunch of trash.

Context

StackExchange Code Review Q#136419, answer score: 8

Revisions (0)

No revisions yet.