I am trying to replace CRLF with LF (see reason @ Read binary stdout data from adb shell?)
A brief summary of the above referenced thread is that basically when piping a screenshot from an Android device via the Android debug bridge, it looks like the line feeds line endings in the data stream are being replaced with carriage return line feeds, therefore I am receiving a corrupted file at the other end of the pipe. What has worked for others is undoing the replacement via code, as below, but it doesn't seem to be working from me.
My code is still spitting a corrupted file... any ideas why?
++ I know the code isn't as clean and efficient as it can be, will fix up after so please hold the comments related to my coding skill, or lack thereof.
Thanks
static void Main(string[] args)
{
// adb shell screencap -p > <path>
string path = @"<filepath>\screen.png";
var fs = new FileStream(path, FileMode.Open);
var fsw = new FileStream(path.Replace(".png", "_fixed.png"), FileMode.Create);
var buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Flush();
fs.Close();
var switched = Repair(buffer);
fsw.Write(switched, 0, switched.Length);
fsw.Flush();
fsw.Close();
Console.WriteLine(buffer.Length);
Console.WriteLine(switched.Length);
Console.Read();
}
static byte[] Repair(byte[] enc)
{
var bstr = new MemoryStream();
for (int i = 0; i < enc.Length; i++)
{
if (enc.Length > i + 1 && enc[i] == 0x0d && enc[i + 1] == 0x0a)
{
bstr.WriteByte(0x0a);
i++;
}
else bstr.WriteByte(enc[i]);
}
bstr.Flush();
bstr.Close();
return bstr.ToArray();
}