4
\$\begingroup\$

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.

JanDotNet
8,5682 gold badges21 silver badges48 bronze badges
asked Jul 30, 2016 at 20:02
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Have you tested that this works? It looks like the call to WriteAsync is incorrect - it should be the number of bytes to write, not the number of chars. \$\endgroup\$ Commented Aug 1, 2016 at 13:15
  • \$\begingroup\$ I'm curious about your requirement, you indicate you want to replace "\r\n" with "\n", but would it be sufficient to simply strip all "\r" characters? Is it valid to have a "\r" character not followed by a "\n"? \$\endgroup\$ Commented Aug 2, 2016 at 8:13

1 Answer 1

8
\$\begingroup\$

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.
Heslacher
50.9k5 gold badges83 silver badges177 bronze badges
answered Aug 1, 2016 at 6:36
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.