Skip to content

Commit

Permalink
Fix for improperly decrementing the remaining number reading messages
Browse files Browse the repository at this point in the history
  • Loading branch information
andyedinborough committed May 15, 2012
1 parent 21517a2 commit 4fdd125
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 2 additions & 0 deletions MailMessage.cs
Expand Up @@ -181,6 +181,8 @@ public class MailMessage : ObjectWHeaders {
(a.IsAttachment ? Attachments : AlternateViews).Add(a);
}
}

data = reader.ReadToEnd(maxLength, Encoding);
}

private static Dictionary<string, int> _FlagCache = System.Enum.GetValues(typeof(Flags)).Cast<Flags>().ToDictionary(x => x.ToString(), x => (int)x, StringComparer.OrdinalIgnoreCase);
Expand Down
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Expand Up @@ -33,5 +33,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.6.1.0")]
[assembly: AssemblyFileVersion("1.6.1.0-beta2")]
[assembly: AssemblyFileVersion("1.6.1-beta3")]
[assembly: InternalsVisibleTo("Tests")]
14 changes: 9 additions & 5 deletions Utilities.cs
Expand Up @@ -12,30 +12,34 @@ internal static class Utilities {
private static CultureInfo _enUsCulture = CultureInfo.GetCultureInfo("en-US");

internal static string ReadLine(this Stream stream, ref int maxLength, Encoding encoding) {
stream.ReadTimeout = 10000;
var maxLengthSpecified = maxLength > 0;
byte b;
using (var mem = new MemoryStream()) {
while (true) {
b = (byte)stream.ReadByte();
if (maxLength > 0) maxLength--;
if (b == 10 || b == 13) {
if (mem.Length == 0 && b == 10) continue;
break;
} else mem.WriteByte(b);
if (maxLength > 0 && mem.Length == maxLength) break;
if (maxLengthSpecified && maxLength == 0) break;
}
if (maxLength > 0) maxLength -= (int)mem.Length;
return encoding.GetString(mem.ToArray());
}
}

internal static string ReadToEnd(this Stream stream, int maxLength, Encoding encoding) {
stream.ReadTimeout = 10000;
int read = 1;
byte[] buffer = new byte[8192];
using (var mem = new MemoryStream()) {
while (read > 0) {
read = stream.Read(buffer, 0, maxLength == 0 ? buffer.Length : Math.Min(maxLength - (int)mem.Length, buffer.Length));
do {
var length = maxLength == 0 ? buffer.Length : Math.Min(maxLength - (int)mem.Length, buffer.Length);
read = stream.Read(buffer, 0, length);
mem.Write(buffer, 0, read);
if (maxLength > 0 && mem.Length == maxLength) break;
}
} while (read > 0);
return encoding.GetString(mem.ToArray());
}
}
Expand Down

0 comments on commit 4fdd125

Please sign in to comment.