Source code modification-convertencoding

Source: Internet
Author: User
 

Using system;
Using system. Collections. Generic;
Using system. IO;
Using system. text;

Namespace source code modification
{
Public class convertencodingtoutf8: ioperatesource
{
Private int amountofhasconverted;
Public int amountofhasconverted
{
Get
{
Return amountofhasconverted;
}
}

Public convertencodingtoutf8 ()
{
Amountofhasconverted = 0;
}

Private encoding GetType (fileinfo sourcefileinfo)
{
Filestream FS = new filestream (sourcefileinfo. fullname, filemode. Open, fileaccess. Read );
Binaryreader reader = new binaryreader (FS, system. Text. encoding. Default );
Byte [] Ss = reader. readbytes (3 );
Reader. Close ();

Encoding encoding;
If (ss [0]> = 0xef)
{
If (ss [0] = 0xef & SS [1] = 0xbb & SS [2] = 0xbf)
{
Encoding = encoding. utf8;
}
Else if (ss [0] = 0xfe & SS [1] = 0xff)
{
Encoding = encoding. bigendianunicode;
}
Else if (ss [0] = 0xff & SS [1] = 0xfe)
{
Encoding = encoding. Unicode;
}
Else
{
Encoding = encoding. default;
}
}
Else
{
Encoding = encoding. default;
}

FS. Close ();

Return encoding;
}

Override public void go (fileinfo sourcefileinfo)
{
Encoding encoding = GetType (sourcefileinfo );

If (encoding! = Encoding. utf8)
{
++ Amountofhasconverted;
Streamreader reader = new streamreader (sourcefileinfo. fullname, encoding );
String Buffer = reader. readtoend ();
Reader. Close ();

Streamwriter writer = new streamwriter (sourcefileinfo. fullname, false, encoding. utf8 );
Writer. Write (buffer );
Writer. Close ();
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.