Mono源代码学习笔记:Console类(五)

CStreamReader 类 (internal class)

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

001:  //
002:  // System.CStreamReader
003:  //
004:  // Authors:
005:  //   Dietmar Maurer ([email protected])
006:  //   Miguel de Icaza ([email protected])
007:  //   Dick Porter ([email protected])
008:  //   Sebastien Pouliot  <[email protected]>
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 ([email protected])
006:  //   Paolo Molaro ([email protected])
007:  //   Dick Porter ([email protected])
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 中被使用。

版权声明:本文为博主http://www.zuiniusn.com原创文章,未经博主允许不得转载。

时间: 2024-08-29 03:06:02

Mono源代码学习笔记:Console类(五)的相关文章

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

NullStream 类 (internal class) 下面就是 mcs/class/corlib/System.IO/NullStream.cs: 01: namespace System.IO 02: { 03: class NullStream : Stream 04: { 05: public override bool CanRead { get { return true; } } 06: public override bool CanSeek { get { return t

Mono源代码学习笔记:Console类(一)

前言 我们知道,Mono 是 .NET Framework 跨平台的开源实现.Mono 的源代码就是金矿,等待我们去挖掘. 目前 Mono 的最新版本是 Mono 2.8.2,可以到 http://ftp.novell.com/pub/mono/sources/mono/ 下载 mono-2.8.2.tar.bz2,文件大小是30MB.可以参阅"在 Ubuntu 10.10 操作系统安装 Mono 2.8.2"一文. 现在,让我们来看看 Mono 是如何实现 .NET Framewor

Mono源代码学习笔记:Console类(六)

Unix 终端的基础知识 许多 Unix 系统使用终端.但是在今天的许多情况下,终端也许是一个运行终端程序的 PC 机.从历史上来说,不同的生产商提供了大量的硬件终端.Linux 操作系统包含一个环境变量 TERM,用来表示我们正在使用的终端的类型,如下所示: [email protected]:~$ w 16:35:13 up 6 days, 7:36, 2 users, load average: 0.62, 0.34, 0.25 USER TTY FROM [email protected

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

Buffer 类 (public static class) 下面就是 mcs/class/corlib/System/Buffer.cs: 001: // 002: // System.Buffer.cs 003: // 004: // Authors: 005: // Paolo Molaro ([email protected]) 006: // Dan Lewis ([email protected]) 007: // 008: // (C) 2001 Ximian, Inc. http

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

NullStream 类 (internal class) 以下就是 mcs/class/corlib/System.IO/NullStream.cs: 01: namespace System.IO 02: { 03: class NullStream : Stream 04: { 05: public override bool CanRead { get { return true; } } 06: public override bool CanSeek { get { return t

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

Buffer 类 (public static class) 以下就是 mcs/class/corlib/System/Buffer.cs: 001: // 002: // System.Buffer.cs 003: // 004: // Authors: 005: // Paolo Molaro ([email protected]) 006: // Dan Lewis ([email protected]) 007: // 008: // (C) 2001 Ximian, Inc. http

Swift学习笔记:类和结构

一.类和结构的异同 类和结构有一些相似的地方,它们都可以: 1. 定义一些可以赋值的属性: 2. 定义具有功能性的方法 3. 定义下标,使用下标语法 4. 定义初始化方法来设置初始状态 5. 在原实现方法上的可扩展性 根据协议提供某一特定类别的基本功能 1. 类还有一些结构不具备的特性: 2. 类的继承性 3. 对类实例实时的类型转换 4. 析构一个类的实例使之释放空间 5. 引用计数,一个类实例可以有多个引用 1. 定义语法 struct Name{ let firstName = "&quo

C++ Primer 学习笔记_24_类与数据抽象(10)--static 与单例模式、auto_ptr与单例模式、const成员函数、const 对象、mutable修饰符

C++ Primer 学习笔记_24_类与数据抽象(10)--static 与单例模式.auto_ptr与单例模式.const成员函数.const 对象.mutable修饰符 前言 [例]写出面向对象的五个基本原则? 解答:单一职责原则,开放封闭原则,依赖倒置原则,接口隔离原则和里氏替换原则 里氏替换原则:子类型必须能够替换他们的基类型. 设计模式分为三种类型:创建型模式.结构型模式和行为型模式 一.static 与单例模式 1.单例模式 单例模式的意图:保证一个类仅有一个实例,并提供一个访问它

C++ Primer 学习笔记_15_类与数据抽象(1)_类的定义和声明

C++ Primer 学习笔记_15_类与数据抽象(1)_类的定义和声明 在C++中,用类来定义自己的抽象数据类型.通过定义类型来对应所要解决的问题中的各种概念,可以使我们更容易编写.调试和修改程序.可以使得自己定义的数据类型用起来与内置类型一样容易和直观. 看一下Sales_item类: class Sales_item { private: std::string isbn; unsigned units_sold; double revenue; public: double ave_pr