CStreamReader 类 (internal class)

下面就是 mcs/class/corlib/System/CStreamReader.cs:

001:  //
002:  // System.CStreamReader
003:  //
004:  // Authors:
005:  //   Dietmar Maurer (dietmar@ximian.com)
006:  //   Miguel de Icaza (miguel@ximian.com)
007:  //   Dick Porter (dick@ximian.com)
008:  //   Sebastien Pouliot  <sebastien@ximian.com>
009:  //
010:  // (C) Ximian, Inc.  http://www.ximian.com
011:  // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
012:  //
013:  // Permission is hereby granted, free of charge, to any person obtaining
014:  // a copy of this software and associated documentation files (the
015:  // "Software"), to deal in the Software without restriction, including
016:  // without limitation the rights to use, copy, modify, merge, publish,
017:  // distribute, sublicense, and/or sell copies of the Software, and to
018:  // permit persons to whom the Software is furnished to do so, subject to
019:  // the following conditions:
020:  //
021:  // The above copyright notice and this permission notice shall be
022:  // included in all copies or substantial portions of the Software.
023:  //
024:  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
025:  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
026:  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
027:  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
028:  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
029:  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
030:  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
031:  //
032:  #if !NET_2_1
033:  using System.Text;
034:  using System.Runtime.InteropServices;
035:
036:  namespace System.IO {
037:      class CStreamReader : StreamReader {
038:          TermInfoDriver driver;
039:
040:          public CStreamReader(Stream stream, Encoding encoding)
041:              : base (stream, encoding)
042:          {
043:              driver = (TermInfoDriver) ConsoleDriver.driver;
044:          }
045:
046:          public override int Peek ()
047:          {
048:              try {
049:                  return base.Peek ();
050:              } catch (IOException) {
051:              }
052:
053:              return -1;
054:          }
055:
056:          public override int Read ()
057:          {
058:              try {
059:                  ConsoleKeyInfo key = Console.ReadKey ();
060:                  return key.KeyChar;
061:              } catch (IOException) {
062:              }
063:
064:              return(-1);
065:          }
066:
067:          public override int Read ([In, Out] char [] dest, int index, int count)
068:          {
069:              if (dest == null)
070:                  throw new ArgumentNullException ("dest");
071:              if (index < 0)
072:                  throw new ArgumentOutOfRangeException ("index", "< 0");
073:              if (count < 0)
074:                  throw new ArgumentOutOfRangeException ("count", "< 0");
075:              // ordered to avoid possible integer overflow
076:              if (index > dest.Length - count)
077:                  throw new ArgumentException ("index + count > dest.Length");
078:
079:              try {
080:                  return driver.Read (dest, index, count);
081:              } catch (IOException) {
082:              }
083:
084:              return 0;
085:          }
086:
087:          public override string ReadLine ()
088:          {
089:              try {
090:                  return driver.ReadLine ();
091:              } catch (IOException) {
092:              }
093:
094:              return null;
095:          }
096:
097:          public override string ReadToEnd ()
098:          {
099:              try {
100:                  return (base.ReadToEnd ());
101:              } catch (IOException) {
102:              }
103:
104:              return null;
105:          }
106:      }
107:  }
108:  #endif

上述源程序定义了 CStreamReader 类。该类位于 System.IO 命名空间中,继承自 StreamReader 类,是 internal 的,只能在本程序集内部使用。我想 CStreamReader 这个名称应该意味着 Console Stream Reader。

第 2 行的注释“System.CStreamReader”有误,应改为“System.IO.CStreamReader”。此外,CStreamReader.cs 放在 mcs/class/corlib/System 目录下也不合理,应该放在 mcs/class/corlib/System.IO 目录下才对。

第 38 行定义了一个类型为 TermInfoDriver 的私有实例字段 driver。

第 40 行到第 44 行的公共构造函数用它的两个参数调用其基类 StreamReader 的相应构造函数,然后对 driver 字段进行赋值。

第 46 行到第 54 行以及第 97 行到第 105 行的两个公共方法重写了基类 StreamReader 对应的虚方法,并简单地调用基类的对应的虚方法,捕获并忽略 IOException 异常。

第 56 行到第 65 行的 Read 公共方法重写了基类 StreamReader 对应的虚方法,通过调用 Console 类的公共静态方法 ReadKey 来达到目的,同样捕获并忽略 IOException 异常。

第 67 行到第 95 行的两个公共方法重写了基类 StreamReader 对应的虚方法,通过私有实例字段 driver 调用 TermInfoDriver 类相应的公共实例方法来达到目的,同样捕获并忽略 IOException 异常。

在 Console.dll 项目中,CStreamReader 类在 Console.cs 中被使用。

CStreamWriter 类 (internal class)

下面就是 mcs/class/corlib/System/CStreamWriter.cs:

001:  //
002:  // System.CStreamWriter
003:  //
004:  // Authors:
005:  //   Dietmar Maurer (dietmar@ximian.com)
006:  //   Paolo Molaro (lupus@ximian.com)
007:  //   Dick Porter (dick@ximian.com)
008:  //
009:  // (c) 2006 Novell, Inc.  http://www.novell.com
010:  //
011:
012:  //
013:  // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
014:  //
015:  // Permission is hereby granted, free of charge, to any person obtaining
016:  // a copy of this software and associated documentation files (the
017:  // "Software"), to deal in the Software without restriction, including
018:  // without limitation the rights to use, copy, modify, merge, publish,
019:  // distribute, sublicense, and/or sell copies of the Software, and to
020:  // permit persons to whom the Software is furnished to do so, subject to
021:  // the following conditions:
022:  //
023:  // The above copyright notice and this permission notice shall be
024:  // included in all copies or substantial portions of the Software.
025:  //
026:  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
027:  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
028:  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
029:  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
030:  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
031:  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
032:  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
033:  //
034:  #if !NET_2_1
035:  using System;
036:  using System.Text;
037:
038:  namespace System.IO {
039:      class CStreamWriter : StreamWriter {
040:          TermInfoDriver driver;
041:
042:          public CStreamWriter (Stream stream, Encoding encoding)
043:              : base (stream, encoding)
044:          {
045:              driver = (TermInfoDriver) ConsoleDriver.driver;
046:          }
047:
048:          public override void Write (char [] buffer, int index, int count)
049:          {
050:              if (count <= 0)
051:                  return;
052:
053:              if (!driver.Initialized) {
054:                  try {
055:                      base.Write (buffer, index, count);
056:                  } catch (IOException) {
057:                  }
058:
059:                  return;
060:              }
061:
062:              lock (this) {
063:                  int last = index + count;
064:                  int i = index;
065:                  int n = 0;
066:                  char c;
067:
068:                  do {
069:                      c = buffer [i++];
070:
071:                      if (driver.IsSpecialKey (c)) {
072:                          // flush what we have
073:                          if (n > 0) {
074:                              try {
075:                                  base.Write (buffer, index, n);
076:                              } catch (IOException) {
077:                              }
078:
079:                              n = 0;
080:                          }
081:
082:                          // write the special key
083:                          driver.WriteSpecialKey (c);
084:
085:                          index = i;
086:                      } else {
087:                          n++;
088:                      }
089:                  } while (i < last);
090:
091:                  if (n > 0) {
092:                      // write out the remainder of the buffer
093:                      try {
094:                          base.Write (buffer, index, n);
095:                      } catch (IOException) {
096:                      }
097:                  }
098:              }
099:          }
100:
101:          public override void Write (char val)
102:          {
103:              lock (this) {
104:                  try {
105:                      if (driver.IsSpecialKey (val))
106:                          driver.WriteSpecialKey (val);
107:                      else
108:                          InternalWriteChar (val);
109:                  } catch (IOException) {
110:                  }
111:              }
112:          }
113:
114:          public void WriteKey (ConsoleKeyInfo key)
115:          {
116:              lock (this) {
117:                  ConsoleKeyInfo copy = new ConsoleKeyInfo (key);
118:                  if (driver.IsSpecialKey (copy))
119:                      driver.WriteSpecialKey (copy);
120:                  else
121:                      InternalWriteChar (copy.KeyChar);
122:              }
123:          }
124:
125:          public void InternalWriteString (string val)
126:          {
127:              try {
128:                  base.Write (val);
129:              } catch (IOException) {
130:              }
131:          }
132:
133:          public void InternalWriteChar (char val)
134:          {
135:              try {
136:                  base.Write (val);
137:              } catch (IOException) {
138:              }
139:          }
140:
141:          public void InternalWriteChars (char [] buffer, int n)
142:          {
143:              try {
144:                  base.Write (buffer, 0, n);
145:              } catch (IOException) {
146:              }
147:          }
148:
149:          public override void Write (char [] val)
150:          {
151:              Write (val, 0, val.Length);
152:          }
153:
154:          public override void Write (string val)
155:          {
156:              if (val == null)
157:                  return;
158:
159:              if (driver.Initialized)
160:                  Write (val.ToCharArray ());
161:              else {
162:                  try {
163:                      base.Write (val);
164:                  } catch (IOException){
165:
166:                  }
167:              }
168:          }
169:      }
170:  }
171:  #endif

上述源程序定义了 CStreamWriter 类。该类位于 System.IO 命名空间中,继承自 StreamWriter 类,是 internal 的,只能在本程序集内部使用。我想 CStreamWriter 这个名称应该意味着 Console Stream Writer。

第 2 行的注释“System.CStreamWriter”有误,应改为“System.IO.CStreamWriter”。此外,CStreamWriter.cs 放在 mcs/class/corlib/System 目录下也不合理,应该放在 mcs/class/corlib/System.IO 目录下才对。

第 40 行定义了一个类型为 TermInfoDriver 的私有实例字段 driver。

第 42 行到第 46 行的公共构造函数用它的两个参数调用其基类 StreamWriter 的相应构造函数,然后对 driver 字段进行赋值。

第 48 到第 99 行的 Write 公共方法重写了基类 StreamWriter 对应的虚方法。如果检查到私有实例字段 driver 末初始化的话,就通过调用基类对应的虚方法来实现其功能,捕获并忽略 IOException 异常。否则的话,还通过 driver 字段对要写入的字符数组中的特殊字符进行特殊处理。

第 101 行到第 168 行的七个公共方法同样在调用基类的方法时捕获并忽略 IOException 异常。也在必要时通过 driver 字段对特殊字符进行特殊处理。

在 Console.dll 项目中,CStreamWriter 类在 Console.cs 和 TermInfoDriver.cs 中被使用。

Mono源代码学习笔记:Console类(五)相关推荐

  1. Mono源代码学习笔记:Console类(四)

    NullStream 类 (internal class) 下面就是 mcs/class/corlib/System.IO/NullStream.cs: 01: namespace System.IO ...

  2. Mono源码学习笔记:Console类(四)

    NullStream 类 (internal class) 以下就是 mcs/class/corlib/System.IO/NullStream.cs: 01: namespace System.IO ...

  3. Mono源码学习笔记:Console类(三)

    Buffer 类 (public static class) 以下就是 mcs/class/corlib/System/Buffer.cs: 001: // 002: // System.Buffer ...

  4. 机器学习理论《统计学习方法》学习笔记:第五章 决策树

    机器学习理论<统计学习方法>学习笔记:第五章 决策树 决策树 5.1 决策树模型与学习 5.1.1 决策树模型 5.1.2 决策树与if-then规则 5.1.3 决策树与条件概率分布 5 ...

  5. python面向对象编程72讲_2020-07-22 Python学习笔记27类和面向对象编程

    一些关于自己学习Python的经历的内容,遇到的问题和思考等,方便以后查询和复习. 声明:本人学习是在扇贝编程通过网络学习的,相关的知识.案例来源于扇贝编程.如果使用请说明来源. 第27关 类与面向对 ...

  6. JavaScript学习笔记(十五)

    JavaScript学习笔记(十五) 事件 事件是DOM(文档对象模型)的一部分.事件流就是事件发生顺序,这是IE和其他浏览器在事件支持上的主要差别. 一.事件流 1.冒泡型事件 IE上的解决方案就是 ...

  7. OpenCV学习笔记(十五):图像仿射变换:warpAffine(),getRotationMatrix2D()

    OpenCV学习笔记(十五):图像仿射变换:warpAffine(),getRotationMatrix2D() 一个任意的仿射变换都能表示为乘以一个矩阵(线性变换)接着再加上一个向量(平移)的形式. ...

  8. eos 源代码学习笔记一

    文章目录 eos 源代码学习笔记 1.eos 中的常见合约类型 2.语言环境局部( locale )变量的使用简介(目的是通过 gettext 软件包 来实现软件的全球化) 3.eos 源代码的一些优 ...

  9. MATLAB学习笔记(十五)

    MATLAB学习笔记(十五) 一.非线性方程求解与函数极值计算 1.1 非线性方程数值求解 1.2 函数极值的计算 1.2.1 无约束最优化问题 1.2.2 有约束最优化问题 一.非线性方程求解与函数 ...

最新文章

  1. plsql设置字段可为空_2015最新整理PLSQL常用设置
  2. python项目实战:20行代码画一朵好看又有趣的小花花
  3. [POJ 3270]Cow Sorting
  4. ​十六周一次课(4月11日) 学习完成 18.11 LVS DR模式搭建 18.12 keepalived + LVS
  5. python集合类型变量_Python小白学习之路(八)—【变量】【基本数据类型分类】【集合】【集合的功能】...
  6. java连接mysql通用方法_jdbc操作数据库通用方法
  7. IDC MarketScape:华为云IoT物联网平台位居领导者象限
  8. 求两个点的欧氏距离_数据智能系列(五)| 距离的秘密
  9. 一张图看懂字节跳动8年创业史,太励志了吧
  10. [HDOJ]1018. Big Number
  11. JavaScript中数组去重汇总
  12. (转)JAVA泛型通配符T,E,K,V区别,T以及Class,Class的区别
  13. 二维数组转化稀疏数组
  14. 关于console.log() 打印得引用类型得数据得相关问题
  15. protobuffer中string和bytes类型
  16. matlab latex emf 乱码,latex 使用中的一些问题
  17. 文本框里面加删除按钮
  18. php 微信代扣开发步骤,【微信支付】微信代扣开发者文档
  19. python泰勒公式_泰勒展开式利用python数值方法证明
  20. 孟子:生于忧患,死于安乐

热门文章

  1. 【四】php字符串操作
  2. 进程已不存在,但端口仍被占用
  3. .net framework处理xml
  4. Python TabError inconsistent use of tabs and spaces in indentation 错误问题描述以及解决
  5. android小应用,只能播放一首歌的应用
  6. Struts2中动态的指定返回的结果集
  7. C# EntityFramework连接MySQL (DbFirst)
  8. CSS——FC(BFC/IFC/FFC/GFC)超详细版+原理案例分析
  9. 北京理工大学计算机课答案,北京理工大学计算机专业考研课程模拟试题.doc
  10. import qs from qs 安装_Python 导包难道你只会个 import 吗?