/// <Summary>
/// Detects character encoding classes
/// <Seealso cref = "System. IO. Stream"/>
/// <Seealso cref = "System. Uri"/>
/// <Seealso cref = "System. IO. FileInfo"/>
/// </Summary>
/// <Remarks>
/// <! [CDATA [
/// <Strong> FileEncoder </strong> is used to detect <see cref = "Uri"/> and <see cref = "System. IO. fileInfo "/>, <see cref =" sbyte "/> encoding of byte arrays.
/// Create By lion <br/>
/// <Br/>
/// Support. Net Framework v1.1.4322 <br/>
/// WebSite: www.lionsky.net (lion-a AT sohu.com) <br/>
///]>
/// </Remarks>
Public class FileEncoder
{
# Region Fields .....
// Frequency tables to hold the GB, Big5, and EUC-TW character
// Frequencies
Internal static int [] [] GBFreq = new int [94] [];
Internal static int [] [] GBKFreq = new int [0, 126] [];
Internal static int [] [] Big5Freq = new int [94] [];
Internal static int [] [] EUC_TWFreq = new int [94] [];
Internal static string [] nicename = new string []
{
"GB2312", "GBK", "HZ", "Big5", "CNS 11643"
, "ISO 2022CN", "UTF-8", "Unicode", "ASCII", "OTHER"
};
# Endregion
# Region Methods .....
/// <Summary>
/// Initialize the <see cref = "IdentifyEncoding"/> instance
/// </Summary>
Public FileEncoder ()
{
Initialize_Frequencies ();
}
# Region GetEncodingName .....
/// <Summary>
/// Determine the encoding type from the specified <see cref = "Uri"/>
/// </Summary>
/// <Param name = "testurl"> <see cref = "Uri"/> </param>
/// <Returns> return encoding type ("GB2312", "GBK", "HZ", "Big5", "CNS 11643", "ISO 2022CN", "UTF-8 ", "Unicode", "ASCII", "OTHER") </returns>
/// <Example>
/// The following example shows how to call the <see cref = "GetEncodingName"/> method:
/// <Code>
/// IdentifyEncoding ide = new IdentifyEncoding ();
/// Response. Write (ide. GetEncodingName (new Uri ("http://china5.nikkeibp.co.jp/china/news/com/200307/pr_com200307170131.html ")));
/// </Code>
/// </Example>
Public virtual string GetEncodingName (System. Uri testurl)
{
Sbyte [] rawtext = new sbyte [2, 1024];
Int bytesread = 0, byteoffset = 0;
System. IO. Stream chinesestream;
Try
{
Chinesestream = System. Net. WebRequest. Create (testurl. AbsoluteUri). GetResponse (). GetResponseStream ();
While (bytesread = ReadInput (chinesestream, ref rawtext, byteoffset, rawtext. Length-byteoffset)> 0)
{
Byteoffset + = bytesread;
}
Chinesestream. Close ();
}
Catch (System. Exception e)
{
System. Console. Error. WriteLine ("Error loading or using URL" + e. ToString ());
}
Return GetEncodingName (rawtext );
}
/// <Summary>
/// Determine the encoding type from the specified <see cref = "System. IO. FileInfo"/>
/// </Summary>
/// <Param name = "testfile"> <see cref = "System. IO. FileInfo"/> </param>
/// <Returns> return encoding type ("GB2312", "GBK", "HZ", "Big5", "CNS 11643", "ISO 2022CN", "UTF-8 ", "Unicode", "ASCII", "OTHER") </returns>
/// <Example>
/// The following example shows how to call the <see cref = "GetEncodingName"/> method:
/// <Code>
/// IdentifyEncoding ide = new IdentifyEncoding ();
/// Response. Write (ide. GetEncodingName (new System. IO. FileInfo (@ "C: est.txt ")));
& N