Guest User

Netduino StreamReader replacement

a guest
Apr 12th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using Microsoft.SPOT;
  5.  
  6. namespace Netduino_ReadFile
  7. {
  8.     // Only works with ASCII streams
  9.     public class FastLineReader
  10.     {
  11.         protected Stream mInputStream;
  12.         protected byte[] mFileBuffer;
  13.         protected int mFileBufferPos;
  14.         protected int mFileBufferLen;
  15.         protected int mBufferSize;
  16.  
  17.         public bool EndOfStream { get; protected set; }
  18.  
  19.         public FastLineReader(Stream s, int bufferSize = 512)
  20.         {
  21.             mInputStream = s;
  22.             mBufferSize = bufferSize;
  23.             mFileBuffer = new byte[bufferSize];
  24.             mFileBufferPos = 0;
  25.             mFileBufferLen = 0;
  26.             EndOfStream = false;
  27.         }
  28.  
  29.         public String ReadLine()
  30.         {
  31.             var lineBuffer = new char[mBufferSize];
  32.             int lineBufferPos = 0;
  33.  
  34.             bool foundLF = false;
  35.             while(!foundLF)
  36.             {
  37.                 if (mFileBufferPos >= mFileBufferLen)
  38.                 {
  39.                     mFileBufferPos = 0;
  40.                     mFileBufferLen = mInputStream.Read(mFileBuffer, mFileBufferPos, mFileBuffer.Length);
  41.                     if (mFileBufferLen == 0)
  42.                     {
  43.                         EndOfStream = true;
  44.                         break; // empty file or we're finished
  45.                     }
  46.                 }
  47.  
  48.                 if (lineBufferPos >= lineBuffer.Length - 1)
  49.                 {
  50.                     var tempBuffer = new char[lineBuffer.Length + mFileBufferLen];
  51.                     Array.Copy(lineBuffer, tempBuffer, lineBuffer.Length);
  52.                     lineBuffer = tempBuffer;
  53.                 }
  54.  
  55.                 int lfLocation = mFileBufferLen - 1;
  56.                 for (int x = mFileBufferPos; x < mFileBufferLen; ++x)
  57.                 {
  58.                     if (mFileBuffer[x] == '\n')
  59.                     {
  60.                         lfLocation = x;
  61.                         foundLF = true;
  62.                         break;
  63.                     }
  64.                 }
  65.  
  66.                 for (int x = mFileBufferPos; x <= lfLocation; ++x)
  67.                 {
  68.                     lineBuffer[lineBufferPos++] = (char)mFileBuffer[x];
  69.                 }
  70.                 mFileBufferPos = lfLocation + 1;
  71.             }
  72.  
  73.             return new String(lineBuffer, 0, lineBufferPos);
  74.         }
  75.     }
  76. }
Add Comment
Please, Sign In to add comment