Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

Shadi

Member Since 09 Jan 2013
Offline Last Active Jul 09 2013 10:30 PM
-----

#48344 StreamReader.ReadLine cannot read larger files

Posted by Shadi on 12 April 2013 - 10:00 PM

I tracked it down in the Netduino source code:

 

StreamReader.Peek()

m_curBufPos is 511

m_curBufLen is 512

m_buffer is byte[512]

 

[color=#0000FF;]   if[/color] (m_curBufPos == m_curBufLen || (([color=#ff0000;]m_buffer[m_curBufPos + 1][/color] & 0x80) != 0 && m_curBufPos + 3 > m_curBufLen))

 

As you know, in a 512 length array, there is no position 512.

So if we change the code to:

  [color=#0000FF;]if[/color] (m_curBufPos <= (m_curBufLen - 1) || ((m_buffer[m_curBufPos + 1] & 0x80) != 0 && m_curBufPos + 3 > m_curBufLen))

 

There is also an endless loop inside Peek():

[color=#008000;]// Retry read until response timeout expires[/color]

[color=#0000FF;]if [/color](m_stream.Length > 0) [color=#0000FF;]while[/color] (i < m_buffer.Length && (noRead = m_stream.Read(m_buffer, i, ([color=#0000FF;]int[/color])(m_buffer.Length - i < m_stream.Length ? m_buffer.Length - i : m_stream.Length))) == 0)
{
  i += noRead; [color=#ff0000;]// gets stuck here[/color]
}

}

 

Fixing that with:

[color=#0000FF;]if[/color] (m_stream.Length > 0 && (m_stream.Position != m_stream.Length))

 

However, this probably breaks if you are reading from a network stream or something.

 

Anyways, here's a quick & dirty line reader:

http://pastebin.com/GpNy0Yr1




#48338 StreamReader.ReadLine cannot read larger files

Posted by Shadi on 12 April 2013 - 07:16 PM

I have confirmed this defect in StreamReader.

 

It looks like you will have to read the filestream directly to get around it.

 

[color=#0000FF;]int[/color] lineCount = 0;
[color=#0000FF;]var[/color] files = System.IO.[color=#2b91af;]Directory[/color].GetFiles([color=#a31515;]"SD"[/color]);
[color=#0000FF;]using[/color] ([color=#0000FF;]var[/color] fs = System.IO.[color=#2b91af;]File[/color].OpenRead(files[0]))
{
[color=#0000FF;]var[/color] buffer = [color=#0000FF;]new[/color] [color=#0000FF;]byte[/color][1024];
[color=#0000FF;]int[/color] readLen = 0;
[color=#0000FF;]while[/color] ((readLen = fs.Read(buffer, 0, buffer.Length)) > 0)
{
[color=#0000FF;]for[/color] ([color=#0000FF;]int[/color] x = 0; x < readLen; ++x)
{
[color=#0000FF;]if[/color] (buffer[x] == [color=#a31515;]'r'[/color])
{
lineCount++;
}
}
}
}
[color=#2b91af;]Debug[/color].Print(files[0] + [color=#a31515;]" - Lines: "[/color] + lineCount.ToString());




home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.