手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

动态修改.Net StreamReader Encoding编码

来源:互联网 作者:west263.com 时间:2008-02-22
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

base_stream = stream;
input_buffer = new byte [buffer_size];
this.buffer_size = buffer_size;
this.encoding = encoding;
decoder = encoding.GetDecoder ();

byte [] preamble = encoding.GetPreamble ();
do_checks = detect_encoding_from_bytemarks ? 1 : 0;
do_checks = (preamble.Length == 0) ? 0 : 2;

decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size)];
decoded_count = 0;
pos = 0;
pos_input =0;
}

public virtual Stream BaseStream
{
get
{
return base_stream;
}
}

public virtual Encoding CurrentEncoding
{
get
{
if (encoding == null)
throw new Exception ();
return encoding;
}
set
{
encoding=value;
decoder = encoding.GetDecoder();
decoded_count = pos decoder.GetChars (input_buffer, pos_input, cbEncoded - pos_input, decoded_buffer, pos);
//DiscardBufferedData();
}
}

public override void Close ()
{
Dispose (true);
}

protected override void Dispose (bool disposing)
{
if (disposing && base_stream != null)
base_stream.Close ();

input_buffer = null;
decoded_buffer = null;
encoding = null;
decoder = null;
base_stream = null;
base.Dispose (disposing);
}

//
// Provides auto-detection of the encoding, as well as skipping over
// byte marks at the beginning of a stream.
//
int DoChecks (int count)
{
if ((do_checks & 2) == 2)
{
byte [] preamble = encoding.GetPreamble ();
int c = preamble.Length;
if (count >= c)
{
int i;

for (i = 0; i < c; i )
if (input_buffer [i] != preamble [i])
break;

if (i == c)
return i;
}
}

if ((do_checks & 1) == 1)
{
if (count < 2)
return 0;

if (input_buffer [0] == 0xfe && input_buffer [1] == 0xff)
{
this.encoding = Encoding.BigEndianUnicode;
return 2;
}

if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe)
{
this.encoding = Encoding.Unicode;
return 2;
}

if (count < 3)
return 0;

if (input_buffer [0] == 0xef && input_buffer [1] == 0xbb && input_buffer [2] == 0xbf)
{
this.encoding = Encoding.UTF8;
return 3;
}
}

return 0;
}

public void DiscardBufferedData ()
{
pos = decoded_count = 0;
mayBlock = false;
// Discard internal state of the decoder too.
decoder = encoding.GetDecoder ();
}

int cbEncoded;
int parse_start;
// the buffer is empty, fill it again
private int ReadBuffer ()
{
pos = 0;
pos_input = 0;
cbEncoded = 0;

// keep looping until the decoder gives us some chars
decoded_count = 0;
parse_start = 0;
do
{
cbEncoded = base_stream.Read (input_buffer, 0, buffer_size);

if (cbEncoded == 0)
return 0;

mayBlock = (cbEncoded < buffer_size);
if (do_checks > 0)
{
Encoding old = encoding;
parse_start = DoChecks (cbEncoded);
if (old != encoding)
{
decoder = encoding.GetDecoder ();
}
do_checks = 0;
cbEncoded -= parse_start;
}
decoded_count = decoder.GetChars (input_buffer, parse_start, cbEncoded, decoded_buffer, 0);

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!