Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. public class LineReader : IDisposable
  2. {
  3. private Stream stream;
  4. private BinaryReader reader;
  5.  
  6. public LineReader(Stream stream) { reader = new BinaryReader(stream); }
  7.  
  8. public string ReadLine()
  9. {
  10. StringBuilder result = new StringBuilder();
  11. char lastChar = reader.ReadChar();
  12. // an EndOfStreamException here would propogate to the caller
  13.  
  14. try
  15. {
  16. char newChar = reader.ReadChar();
  17. if (lastChar == 'r' && newChar == 'n')
  18. return result.ToString();
  19.  
  20. result.Append(lastChar);
  21. lastChar = newChar;
  22. }
  23. catch (EndOfStreamException)
  24. {
  25. result.Append(lastChar);
  26. return result.ToString();
  27. }
  28. }
  29.  
  30. public void Dispose()
  31. {
  32. reader.Close();
  33. }
  34. }
  35.  
  36. public class LineReader : BinaryReader
  37. {
  38. private Encoding _encoding;
  39. private Decoder _decoder;
  40.  
  41. const int bufferSize = 1024;
  42. private char[] _LineBuffer = new char[bufferSize];
  43.  
  44. public LineReader(Stream stream, int bufferSize, Encoding encoding)
  45. : base(stream, encoding)
  46. {
  47. this._encoding = encoding;
  48. this._decoder = encoding.GetDecoder();
  49. }
  50.  
  51. public string ReadLine()
  52. {
  53. int pos = 0;
  54.  
  55. char[] buf = new char[2];
  56.  
  57. StringBuilder stringBuffer = null;
  58. bool lineEndFound = false;
  59.  
  60. while(base.Read(buf, 0, 2) > 0)
  61. {
  62. if (buf[1] == 'r')
  63. {
  64. // grab buf[0]
  65. this._LineBuffer[pos++] = buf[0];
  66. // get the 'n'
  67. char ch = base.ReadChar();
  68. Debug.Assert(ch == 'n');
  69.  
  70. lineEndFound = true;
  71. }
  72. else if (buf[0] == 'r')
  73. {
  74. lineEndFound = true;
  75. }
  76. else
  77. {
  78. this._LineBuffer[pos] = buf[0];
  79. this._LineBuffer[pos+1] = buf[1];
  80. pos += 2;
  81.  
  82. if (pos >= bufferSize)
  83. {
  84. stringBuffer = new StringBuilder(bufferSize + 80);
  85. stringBuffer.Append(this._LineBuffer, 0, bufferSize);
  86. pos = 0;
  87. }
  88. }
  89.  
  90. if (lineEndFound)
  91. {
  92. if (stringBuffer == null)
  93. {
  94. if (pos > 0)
  95. return new string(this._LineBuffer, 0, pos);
  96. else
  97. return string.Empty;
  98. }
  99. else
  100. {
  101. if (pos > 0)
  102. stringBuffer.Append(this._LineBuffer, 0, pos);
  103. return stringBuffer.ToString();
  104. }
  105. }
  106. }
  107.  
  108. if (stringBuffer != null)
  109. {
  110. if (pos > 0)
  111. stringBuffer.Append(this._LineBuffer, 0, pos);
  112. return stringBuffer.ToString();
  113. }
  114. else
  115. {
  116. if (pos > 0)
  117. return new string(this._LineBuffer, 0, pos);
  118. else
  119. return null;
  120. }
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement