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 中被使用。