Skip to content

Commit

Permalink
updates for default encoding #54
Browse files Browse the repository at this point in the history
  • Loading branch information
andyedinborough committed Apr 9, 2012
1 parent 84b9296 commit 43508f8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion TextClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class TextClient : IDisposable {
public System.Text.Encoding Encoding { get; set; }

public TextClient() {
Encoding = Utilities.ParseCharsetToEncoding(Utilities.DEFAULT_CHARSET);
Encoding = System.Text.Encoding.Default;
}

internal abstract void OnLogin(string username, string password);
Expand Down
11 changes: 6 additions & 5 deletions Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;

namespace AE.Net.Mail {
internal static class Utilities {
public const string DEFAULT_CHARSET = "ISO-8859-1";

internal static void TryDispose<T>(ref T obj) where T : class, IDisposable {
try {
if (obj != null)
Expand Down Expand Up @@ -207,7 +206,7 @@ internal static string DecodeWords(string encodedWords) {
/// <exception cref="ArgumentNullException">If <paramref name="characterSet"/> is <see langword="null"/></exception>
public static Encoding ParseCharsetToEncoding(string characterSet) {
if (string.IsNullOrEmpty(characterSet))
characterSet = DEFAULT_CHARSET;
return Encoding.Default;

string charSetUpper = characterSet.ToUpperInvariant();
if (charSetUpper.Contains("WINDOWS") || charSetUpper.Contains("CP")) {
Expand All @@ -219,11 +218,13 @@ public static Encoding ParseCharsetToEncoding(string characterSet) {
// Now we hope the only thing left in the characterSet is numbers.
int codepageNumber = int.Parse(charSetUpper, System.Globalization.CultureInfo.InvariantCulture);

return Encoding.GetEncoding(codepageNumber);
return Encoding.GetEncodings().Where(x => x.CodePage == codepageNumber)
.Select(x => x.GetEncoding()).FirstOrDefault() ?? Encoding.Default;
}

// It seems there is no codepage value in the characterSet. It must be a named encoding
return Encoding.GetEncoding(characterSet);
return Encoding.GetEncodings().Where(x => x.Name.Is(characterSet))
.Select(x => x.GetEncoding()).FirstOrDefault() ?? Encoding.Default;
}
#endregion

Expand Down

0 comments on commit 43508f8

Please sign in to comment.