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.
1 Answer 1
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 aUnicodeEncoding
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.
WriteAsync
is incorrect - it should be the number ofbyte
s to write, not the number ofchar
s. \$\endgroup\$