在.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 ();
}
}
}
新聞熱點
疑難解答
圖片精選