在.net framework中streamreader的使用encoding必须在构造器中指定,而且中途完全不可以更改。
在一般的情况下,这不会造成什么问题。一般若是从硬盘读取文件,单一文件内的编码一般都是统一的。即便是发现读错,亦可以关闭streamreader,重启使用新的编码读取。
偏偏偶最近遇到了需要修改编码的需求,而且,我的程序没有关闭重读的机会。因为偶使用的streamreader的basestream是一个network stream,我不可以关闭它……但是network stream传过来的东西很可能包涵不同的编码……gb2312,big5,utf8,iso-8859-1等等……虽然是先得到编码信息,然后再读具体内容,但是,一开始使用的stream reader编码一旦错了,读出来的东西便再也无法恢复……会丢字之类的……
我也不可以在获得编码信息之后,重新建立一个新的stream reader,因为具体内容已经被原来的stream reader给缓冲掉了……
唯一的解决方法,便是自己实现一个可以改变currentencoding属性的stream reader了……
全部从头写起非常不实际,偶是先当了mono的源码,从mono的stream reader实现代码做修改。
stream reader其实很简单,它内部有两个buffer,一个是input buffer,一个是decoded buffer,前者用于缓存从base stream读过来的原始数据,后者用于缓存根据原始数据解码出来后的东西……只要看明白mono的实现中readbuffer这个方法,要动态修改currentencoding也就不是太难了……
我需要处理的网络协议是一个行协议……偶在程序中只调用了streamreader的readline方法,而完全没有使用read的两个方法,这也使得偶动态修改编码容易了许多……
偶的做法是每次调用readline的时候,不仅移动decoded buffer的游标(pos),同时也移动input buffer一个新的游标(pos_input),做法很简单,readline方法需要调用findnexteol移动游标查找换行符号……我在findnexteol方法添加多一行:
int findnexteol ()
{
findnextinputeol();
....
而findnextinputeol这个新的函数,完全是findnexteol的翻版,只是前者处理input buffer,而后者处理decoded buffer……
如此一来,我便可以知道每次readline之后,input buffer中还没有被上层读到的原始数据有哪些了……
然后,再把currentencoding属性添加set的方法:
set
{
encoding=value;
decoder = encoding.getdecoder();
decoded_count = pos + decoder.getchars (input_buffer, pos_input, cbencoded , pos_input, decoded_buffer, pos);
}
设定新编码时,程序便根据input buffer的游标(pos_input)把没有被读到的原始数据重新decode一次,并且替换掉decoded buffer中的内容。
然后,事情就搞定了……甚至不需要对readline方法做任何修改……除了把cbencoded这个变量放到全局里面外……
但是,偶这个修改使得read的两个方法变得完全不可以用……一旦调用了……便会使得input buffer与decoded buffer里面两个游标不同步……下面附上完整的代码,还望有大侠可以帮忙把read的两个方法也给搞定了…… 先谢过……
/
// system.io.streamreader.cs
//
// author:
// dietmar maurer ([email protected])
// miguel de icaza ([email protected])
//
// (c) ximian, inc. http://www.ximian.com
// copyright (c) 2004 novell (http://www.novell.com)
//
//
// copyright (c) 2004 novell, inc (http://www.novell.com)
//
// permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "software"), to deal in the software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the software, and to
// permit persons to whom the software is furnished to do so, subject to
// the following conditions:
//
// the above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the software.
//
// the software is provided "as is", without warranty of any kind,
// express or implied, including but not limited to the warranties of
// merchantability, fitness for a particular purpose and
// noninfringement. in no event shall the authors or copyright holders be
// liable for any claim, damages or other liability, whether in an action
// of contract, tort or otherwise, arising from, out of or in connection
// with the software or the use or other dealings in the software.
//
using system;
using system.text;
using system.runtime.interopservices;
namespace system.io
{
[serializable]
public class dynamicstreamreader : textreader
{
const int defaultbuffersize = 1024;
const int defaultfilebuffersize = 4096;
const int minimumbuffersize = 128;
//
// the input buffer
//
byte [] input_buffer;
//
// the decoded buffer from the above input buffer
//
char [] decoded_buffer;
//
// decoded bytes in decoded_buffer.
//
int decoded_count;
//
// current position in the decoded_buffer
//
int pos;
//
// current position in the input_buffer
//
int pos_input;
//
// the buffer size that we are using
//
int buffer_size;
int do_checks;
encoding encoding;
decoder decoder;
stream base_stream;
bool mayblock;
stringbuilder line_builder;
private class nullstreamreader : dynamicstreamreader
{
public override int peek ()
{
return -1;
}
public override int read ()
{
return -1;
}
public override int read ([in, out] char[] buffer, int index, int count)
{
return 0;
}
public override string readline ()
{
return null;
}
public override string readtoend ()
{
return string.empty;
}
public override stream basestream
{
get { return stream.null; }
}
public override encoding currentencoding
{
get { return encoding.unicode; }
}
}
public new static readonly dynamicstreamreader null = (dynamicstreamreader)(new nullstreamreader());
internal dynamicstreamreader() {}
public dynamicstreamreader(stream stream)
: this (stream, encoding.utf8, true, defaultbuffersize) { }
public dynamicstreamreader(stream stream, bool detect_encoding_from_bytemarks)
: this (stream, encoding.utf8, detect_encoding_from_bytemarks, defaultbuffersize) { }
public dynamicstreamreader(stream stream, encoding encoding)
: this (stream, encoding, true, defaultbuffersize) { }
public dynamicstreamreader(stream stream, encoding encoding, bool detect_encoding_from_bytemarks)
: this (stream, encoding, detect_encoding_from_bytemarks, defaultbuffersize) { }
public dynamicstreamreader(stream stream, encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
{
initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
}
public dynamicstreamreader(string path)
: this (path, encoding.utf8, true, defaultfilebuffersize) { }
public dynamicstreamreader(string path, bool detect_encoding_from_bytemarks)
: this (path, encoding.utf8, detect_encoding_from_bytemarks, defaultfilebuffersize) { }
public dynamicstreamreader(string path, encoding encoding)
: this (path, encoding, true, defaultfilebuffersize) { }
public dynamicstreamreader(string path, encoding encoding, bool detect_encoding_from_bytemarks)
: this (path, encoding, detect_encoding_from_bytemarks, defaultfilebuffersize) { }
public dynamicstreamreader(string path, encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
{
if (null == path)
throw new argumentnullexception("path");
if (string.empty == path)
throw new argumentexception("empty path not allowed");
if (path.indexofany (path.invalidpathchars) != -1)
throw new argumentexception("path contains invalid characters");
if (null == encoding)
throw new argumentnullexception ("encoding");
if (buffer_size <= 0)
throw new argumentoutofrangeexception ("buffer_size", "the minimum size of the buffer must be positive");
string dirname = path.getdirectoryname(path);
if (dirname != string.empty && !directory.exists(dirname))
throw new directorynotfoundexception ("directory '" + dirname + "' not found.");
if (!file.exists(path))
throw new filenotfoundexception("file not found.", path);
stream stream = (stream) file.openread (path);
initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
}
internal void initialize (stream stream, encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
{
if (null == stream)
throw new argumentnullexception ("stream");
if (null == encoding)
throw new argumentnullexception ("encoding");
if (!stream.canread)
throw new argumentexception ("cannot read stream");
if (buffer_size <= 0)
throw new argumentoutofrangeexception ("buffer_size", "the minimum size of the buffer must be positive");
if (buffer_size < minimumbuffersize)
buffer_size = minimumbuffersize;
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);
parse_start = 0;
} while (decoded_count == 0);
return decoded_count;
}
public override int peek ()
{
if (base_stream == null)
throw new objectdisposedexception ("streamreader", "cannot read from a closed streamreader");
if (pos >= decoded_count && (mayblock || readbuffer () == 0))
return -1;
return decoded_buffer [pos];
}
public override int read ()
{
throw new exception("dynamic reader could not read!");
}
public override int read ([in, out] char[] dest_buffer, int index, int count)
{
throw new exception("dynamic reader could not read!");
}
bool foundcr_input;
int findnextinputeol()
{
char c = '/0';
for (; pos_input < cbencoded; pos_input++)
{
c = (char)input_buffer [pos_input];
if (c == '/n')
{
pos_input++;
int res = (foundcr_input) ? (pos_input - 2) : (pos_input - 1);
if (res < 0)
res = 0; // if a new buffer starts with a /n and there was a /r at
// the end of the previous one, we get here.
foundcr_input = false;
return res;
}
else if (foundcr_input)
{
foundcr_input = false;
return pos - 1;
}
foundcr_input = (c == '/r');
}
return -1;
}
bool foundcr;
int findnexteol ()
{
findnextinputeol();
char c = '/0';
for (; pos < decoded_count; pos++)
{
c = decoded_buffer [pos];
if (c == '/n')
{
pos++;
int res = (foundcr) ? (pos - 2) : (pos - 1);
if (res < 0)
res = 0; // if a new buffer starts with a /n and there was a /r at
// the end of the previous one, we get here.
foundcr = false;
return res;
}
else if (foundcr)
{
foundcr = false;
return pos - 1;
}
foundcr = (c == '/r');
}
return -1;
}
public override string readline()
{
if (base_stream == null)
throw new objectdisposedexception ("streamreader", "cannot read from a closed streamreader");
if (pos >= decoded_count && readbuffer () == 0)
return null;
int begin = pos;
int end = findnexteol ();
if (end < decoded_count && end >= begin)
return new string (decoded_buffer, begin, end - begin);
if (line_builder == null)
line_builder = new stringbuilder ();
else
line_builder.length = 0;
while (true)
{
if (foundcr) // don't include the trailing cr if present
decoded_count--;
line_builder.append (new string (decoded_buffer, begin, decoded_count - begin));
if (readbuffer () == 0)
{
if (line_builder.capacity > 32768)
{
stringbuilder sb = line_builder;
line_builder = null;
return sb.tostring (0, sb.length);
}
return line_builder.tostring (0, line_builder.length);
}
begin = pos;
end = findnexteol ();
if (end < decoded_count && end >= begin)
{
line_builder.append (new string (decoded_buffer, begin, end - begin));
if (line_builder.capacity > 32768)
{
stringbuilder sb = line_builder;
line_builder = null;
return sb.tostring (0, sb.length);
}
return line_builder.tostring (0, line_builder.length);
}
}
}
public override string readtoend()
{
if (base_stream == null)
throw new objectdisposedexception ("streamreader", "cannot read from a closed streamreader");
stringbuilder text = new stringbuilder ();
int size = decoded_buffer.length;
char [] buffer = new char [size];
int len;
while ((len = read (buffer, 0, size)) > 0)
text.append (buffer, 0, len);
return text.tostring ();
}
}
}
新闻热点
疑难解答
图片精选