• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
初出茅庐
我的技术之路....每天前进一小步
博客园    首页    新随笔    联系   管理    订阅  订阅

【转】 NET 下的简繁互换

重写了Stream类

需要引入Microsoft.VisualBasic;

class CnToTwStream : Stream
{
    private Stream _sink;
    private MemoryStream _ms;
    private Encoding _encoding;

    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);
    }
}
posted @ 2006-06-12 15:35  Cheek G  阅读(262)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3