using System.Text;
namespace SWModules
{
///
/// The SafeEncoding Class is a child of the UTF8 Encoding class,
/// which implements input checking on the GetChars Methods, that
/// avoids exceptions when the input characters exceed decimal
/// value 127. Any values larger than 127 are converted to
/// carriage returns.
///
public class SafeEncoding : UTF8Encoding
{
private const byte Nonprintreplacement = 13;
private static SafeEncoding _utf8;
///
/// Returns a static instance of the SafeEncoding class
///
public new static SafeEncoding UTF8
{
get { return _utf8 ?? (_utf8 = new SafeEncoding()); }
}
///
/// Checks for values above 127 and replaces with selected character
///
/// The byte array containing the sequence of bytes to decode.
/// A character array containing the results of decoding the specified sequence of bytes.
public static char[] GetChars(byte[] bytes)
{
for (var i = 0; i < bytes.Length; i++)
if (bytes[i] > 127)
bytes[i] = Nonprintreplacement;
try
{
return UTF8Encoding.UTF8.GetChars(bytes);
}
catch
{
return null;
}
}
///
/// Checks for values above 127 and replaces with selected character
///
/// The byte array containing the sequence of bytes to decode.
/// The index of the first byte to decode.
/// The number of bytes to decode.
/// A character array containing the results of decoding the specified sequence of bytes.
public static char[] GetChars(byte[] bytes, int byteIndex, int byteCount)
{
for (var i = byteIndex; i < byteIndex + byteCount; i++)
if (bytes[i] > 127)
bytes[i] = Nonprintreplacement;
try
{
return UTF8Encoding.UTF8.GetChars(bytes, byteIndex, byteCount);
}
catch
{
return null;
}
}
}
}