Mono源代码学习笔记:Console类(五)
来源:互联网 发布:update sql 所有数据 编辑:程序博客网 时间:2024/06/04 19:51
CStreamReader 类 (internal class)
下面就是 mcs/class/corlib/System/CStreamReader.cs:
001: //002: // System.CStreamReader003: //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.com011: // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)012: //013: // Permission is hereby granted, free of charge, to any person obtaining014: // a copy of this software and associated documentation files (the015: // "Software"), to deal in the Software without restriction, including016: // without limitation the rights to use, copy, modify, merge, publish,017: // distribute, sublicense, and/or sell copies of the Software, and to018: // permit persons to whom the Software is furnished to do so, subject to019: // the following conditions:020: // 021: // The above copyright notice and this permission notice shall be022: // 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 OF026: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND027: // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE028: // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION029: // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION030: // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.031: //032: #if !NET_2_1033: 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 overflow076: 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.CStreamWriter003: //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.com010: //011: 012: //013: // Copyright (C) 2004 Novell, Inc (http://www.novell.com)014: //015: // Permission is hereby granted, free of charge, to any person obtaining016: // a copy of this software and associated documentation files (the017: // "Software"), to deal in the Software without restriction, including018: // without limitation the rights to use, copy, modify, merge, publish,019: // distribute, sublicense, and/or sell copies of the Software, and to020: // permit persons to whom the Software is furnished to do so, subject to021: // the following conditions:022: // 023: // The above copyright notice and this permission notice shall be024: // 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 OF028: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND029: // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE030: // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION031: // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION032: // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.033: //034: #if !NET_2_1035: 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 have073: 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 key083: 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 buffer093: 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: else108: 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: else121: 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类(五)
- Mono源代码学习笔记:Console类(四)
- Mono源代码学习笔记:Console类(三)
- Mono学习笔记
- VINS mono 系统学习 五
- Java 学习笔记-IO-Console类
- Swift学习笔记(五)——在Background中启用控制台Console Output模式
- NUnit学习笔记之Mono篇
- NUnit学习笔记之Mono篇
- NUnit学习笔记之Mono篇
- Javascript学习笔记(console对象)
- 《C#入门到精通》学习笔记 -- Console类
- Openstack学习笔记(十五)-Horizon源代码学习笔记(五)
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(五)
- 共同学习Java源代码--数据结构--AbstractList抽象类(五)
- 共同学习Java源代码--数据结构--ArrayList类(五)
- 共同学习Java源代码--数据结构--LinkedList类(五)
- 共同学习Java源代码-数据结构-AbstractMap抽象类(五)
- Gulp挑战Grunt,背后的哲学
- HTTP1.1中CHUNKED编码解析
- Python的迭代器和生成器
- yii rules使用 获取error信息
- Oracle语句优化规则汇总(8)
- Mono源代码学习笔记:Console类(五)
- SQL基础大集合
- OpenSSL库的RSA使用(下)-rsa函数方式
- iOS UI01_Label
- Mono源代码学习笔记:Console类(四)
- Oracle语句优化规则汇总(9)
- scikit-learn:3.4. Model persistence
- C 实现最小二乘,步骤以及代码
- Mono源代码学习笔记:Console类(三)