.net中实现繁简互换

in Develop

重写了Stream类
本站就是这样实现的
需要引入Microsoft.VisualBasic;

class CnToTwStream : Stream
{
private Stream _sink;
private MemoryStream _ms;
private Encoding _encoding;</code>

public CnToTwStream(Stream sink, Encoding encoding)
{
_sink = sink;
_ms = new MemoryStream();
_encoding = encoding;
}

public override bool CanRead
{
get { return false; }
}

public override bool CanSeek
{
get { return false; }
}

public override bool CanWrite
{
get { return true; }
}

public override long Length
{
get { return _ms.Length; }
}

public override long Position
{
get { return _ms.Position; }
set { throw new NotSupportedException(); }
}

public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}

public override long Seek(long offset, System.IO.SeekOrigin direction)
{
throw new NotSupportedException();
}

public override void SetLength(long length)
{
throw new NotSupportedException();
}

public override void Close()
{
_ms.Close();
byte[] buffer_cn = _ms.GetBuffer();
string str_cn = _encoding.GetString(buffer_cn);
string str_tw = Strings.StrConv(str_cn, VbStrConv.TraditionalChinese, 0);
byte[] buffer_tw = _encoding.GetBytes(str_tw);
using (_sink)
{
_sink.Write(buffer_tw, 0, buffer_tw.Length);
}
}

public override void Flush()
{
_ms.Flush();
}

public override void Write(byte[] buffer, int offset, int count)
{
_ms.Write(buffer, offset, count);
}
}
2 Comments

2 Comments

  1. 我觉得还是用JS的方法方便一些

  2. js那是假的啊
    而且现在的js实现都不支持ff的
    庞大的js文件会使文件过大[em14]

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>