95 lines
3.1 KiB
C#
95 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace XYLEM.Communication.ValueEncoders {
|
|
public static class StringEncoderEmail {
|
|
public static Encoding GetDefaultEncoding( ) {
|
|
try {
|
|
return Encoding.GetEncoding( "iso-8859-1"/*Strings._CONFIG_STD_Email_DefaultCharacterEncoding*/ );
|
|
} catch (Exception ex) {
|
|
throw new Exception( typeof( StringEncoderEmail ).ToString( )+ "\n\rGetDefaultEncoding"+ "\n\rWith = " + "iso-8859-1"/*Strings._CONFIG_STD_Email_DefaultCharacterEncoding*/, ex );
|
|
}
|
|
// Legacy code return Encoding.GetEncoding( "iso-8859-1" );
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Standard Windows iso-8859-1-encoding page.
|
|
/// </summary>
|
|
static Encoding s_Encoder = GetDefaultEncoding( ); // Default Fallback
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <param name="lenght"></param>
|
|
/// <param name="CharacterSet">ex. "iso-8859-1"</param>
|
|
/// <returns></returns>
|
|
public static byte[] Encode(string value, int lenght, string CharacterSet) {
|
|
if (value.Length > lenght) {
|
|
throw new ArgumentException( "value.Length is too long", "value" );
|
|
}
|
|
var result = new byte[ lenght ];
|
|
try {
|
|
if (!String.IsNullOrEmpty( CharacterSet )) {
|
|
Encoding.GetEncoding( CharacterSet ).GetBytes( value ).CopyTo( result, 0 );
|
|
return result;
|
|
}
|
|
} catch (Exception ex) {
|
|
throw new Exception( typeof( StringEncoderEmail ).ToString( )+ "\n\rEncode()"+ "\n\rWith = " + "iso-8859-1"/*Strings._CONFIG_STD_Email_DefaultCharacterEncoding*/,ex );
|
|
}
|
|
// Use defalt
|
|
s_Encoder.GetBytes( value ).CopyTo( result, 0 );
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use default characterSet
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <param name="lenght"></param>
|
|
/// <returns></returns>
|
|
public static byte[] Encode(string value, int lenght) { return Encode( value, lenght, null ); }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <param name="lenght"></param>
|
|
/// <param name="CharacterSet">ex. "iso-8859-1"</param>
|
|
/// <returns></returns>
|
|
public static string Decode(byte[] value, int lenght, string CharacterSet) {
|
|
if (value.Length != lenght) {
|
|
throw new ArgumentException( "value.Length wrong lenght", "value" );
|
|
}
|
|
string result = null;
|
|
try {
|
|
if (!String.IsNullOrEmpty( CharacterSet )) {
|
|
result = Encoding.GetEncoding( CharacterSet ).GetString( value );
|
|
}
|
|
} catch (Exception ex) {
|
|
throw new Exception( typeof( StringEncoderEmail ).ToString()+ "\n\rDecode()"+ "\n\rWith = " + "iso-8859-1"/*Strings._CONFIG_STD_Email_DefaultCharacterEncoding*/, ex );
|
|
}
|
|
// Use defalt
|
|
if (result == null) {
|
|
result = s_Encoder.GetString( value );
|
|
}
|
|
var index = result.IndexOf( '\0' );
|
|
if (index >= 0) {
|
|
|
|
return result.Substring( 0, result.IndexOf( '\0' ) );
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use default characterSet
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <param name="lenght"></param>
|
|
/// <returns></returns>
|
|
public static string Decode(byte[] value, int lenght) { return Decode( value, lenght, null ); }
|
|
}
|
|
} |