The Labs \ Source Viewer \ SSCLI \ System.IO \ TextReader

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  TextReader
  18. **
  19. **
  20. ** Purpose: Abstract base class for all Text-only Readers.
  21. ** Subclasses will include StreamReader & StringReader.
  22. **
  23. **
  24. ===========================================================*/
  25. using System;
  26. using System.Text;
  27. using System.Runtime.InteropServices;
  28. using System.Runtime.CompilerServices;
  29. using System.Reflection;
  30. using System.Security.Permissions;
  31. namespace System.IO
  32. {
  33.     // This abstract base class represents a reader that can read a sequential
  34.     // stream of characters. This is not intended for reading bytes -
  35.     // there are methods on the Stream class to read bytes.
  36.     // A subclass must minimally implement the Peek() and Read() methods.
  37.     //
  38.     // This class is intended for character input, not bytes.
  39.     // There are methods on the Stream class for reading bytes.
  40.     [Serializable()]
  41.     [ComVisible(true)]
  42.     public abstract class TextReader : MarshalByRefObject, IDisposable
  43.     {
  44.         public static readonly TextReader Null = new NullTextReader();
  45.        
  46.         protected TextReader()
  47.         {
  48.         }
  49.        
  50.         // Closes this TextReader and releases any system resources associated with the
  51.         // TextReader. Following a call to Close, any operations on the TextReader
  52.         // may raise exceptions.
  53.         //
  54.         // This default method is empty, but descendant classes can override the
  55.         // method to provide the appropriate functionality.
  56.         public virtual void Close()
  57.         {
  58.             Dispose(true);
  59.         }
  60.        
  61.         public void Dispose()
  62.         {
  63.             Dispose(true);
  64.         }
  65.        
  66.         protected virtual void Dispose(bool disposing)
  67.         {
  68.         }
  69.        
  70.         // Returns the next available character without actually reading it from
  71.         // the input stream. The current position of the TextReader is not changed by
  72.         // this operation. The returned value is -1 if no further characters are
  73.         // available.
  74.         //
  75.         // This default method simply returns -1.
  76.         //
  77.         public virtual int Peek()
  78.         {
  79.             return -1;
  80.         }
  81.        
  82.         // Reads the next character from the input stream. The returned value is
  83.         // -1 if no further characters are available.
  84.         //
  85.         // This default method simply returns -1.
  86.         //
  87.         public virtual int Read()
  88.         {
  89.             return -1;
  90.         }
  91.        
  92.         // Reads a block of characters. This method will read up to
  93.         // count characters from this TextReader into the
  94.         // buffer character array starting at position
  95.         // index. Returns the actual number of characters read.
  96.         //
  97.         public virtual int Read(        [In(), Out()]
  98. char[] buffer, int index, int count)
  99.         {
  100.             if (buffer == null)
  101.                 throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
  102.             if (index < 0)
  103.                 throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  104.             if (count < 0)
  105.                 throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
  106.             if (buffer.Length - index < count)
  107.                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
  108.            
  109.             int n = 0;
  110.             do {
  111.                 int ch = Read();
  112.                 if (ch == -1)
  113.                     break;
  114.                 buffer[index + n++] = (char)ch;
  115.             }
  116.             while (n < count);
  117.             return n;
  118.         }
  119.        
  120.         // Reads all characters from the current position to the end of the
  121.         // TextReader, and returns them as one string.
  122.         public virtual string ReadToEnd()
  123.         {
  124.             char[] chars = new char[4096];
  125.             int len;
  126.             StringBuilder sb = new StringBuilder(4096);
  127.             while ((len = Read(chars, 0, chars.Length)) != 0) {
  128.                 sb.Append(chars, 0, len);
  129.             }
  130.             return sb.ToString();
  131.         }
  132.        
  133.         // Blocking version of read. Returns only when count
  134.         // characters have been read or the end of the file was reached.
  135.         //
  136.         public virtual int ReadBlock(        [In(), Out()]
  137. char[] buffer, int index, int count)
  138.         {
  139.             int i;
  140.             int n = 0;
  141.             do {
  142.                 n += (i = Read(buffer, index + n, count - n));
  143.             }
  144.             while (i > 0 && n < count);
  145.             return n;
  146.         }
  147.        
  148.         // Reads a line. A line is defined as a sequence of characters followed by
  149.         // a carriage return ('\r'), a line feed ('\n'), or a carriage return
  150.         // immediately followed by a line feed. The resulting string does not
  151.         // contain the terminating carriage return and/or line feed. The returned
  152.         // value is null if the end of the input stream has been reached.
  153.         //
  154.         public virtual string ReadLine()
  155.         {
  156.             StringBuilder sb = new StringBuilder();
  157.             while (true) {
  158.                 int ch = Read();
  159.                 if (ch == -1)
  160.                     break;
  161.                 if (ch == '\r' || ch == '\n') {
  162.                     if (ch == '\r' && Peek() == '\n')
  163.                         Read();
  164.                     return sb.ToString();
  165.                 }
  166.                 sb.Append((char)ch);
  167.             }
  168.             if (sb.Length > 0)
  169.                 return sb.ToString();
  170.             return null;
  171.         }
  172.        
  173.        
  174.         [HostProtection(Synchronization = true)]
  175.         public static TextReader Synchronized(TextReader reader)
  176.         {
  177.             if (reader == null)
  178.                 throw new ArgumentNullException("reader");
  179.             if (reader is SyncTextReader)
  180.                 return reader;
  181.            
  182.             return new SyncTextReader(reader);
  183.         }
  184.        
  185.         [Serializable()]
  186.         private sealed class NullTextReader : TextReader
  187.         {
  188.             public override int Read(char[] buffer, int index, int count)
  189.             {
  190.                 return 0;
  191.             }
  192.            
  193.             public override string ReadLine()
  194.             {
  195.                 return null;
  196.             }
  197.         }
  198.        
  199.         [Serializable()]
  200.         internal sealed class SyncTextReader : TextReader
  201.         {
  202.             internal TextReader _in;
  203.            
  204.             internal SyncTextReader(TextReader t)
  205.             {
  206.                 _in = t;
  207.             }
  208.            
  209.             [MethodImplAttribute(MethodImplOptions.Synchronized)]
  210.             public override void Close()
  211.             {
  212.                 // So that any overriden Close() gets run
  213.                 _in.Close();
  214.             }
  215.            
  216.             [MethodImplAttribute(MethodImplOptions.Synchronized)]
  217.             protected override void Dispose(bool disposing)
  218.             {
  219.                 // Explicitly pick up a potentially methodimpl'ed Dispose
  220.                 if (disposing)
  221.                     ((IDisposable)_in).Dispose();
  222.             }
  223.            
  224.             [MethodImplAttribute(MethodImplOptions.Synchronized)]
  225.             public override int Peek()
  226.             {
  227.                 return _in.Peek();
  228.             }
  229.            
  230.             [MethodImplAttribute(MethodImplOptions.Synchronized)]
  231.             public override int Read()
  232.             {
  233.                 return _in.Read();
  234.             }
  235.            
  236.             [MethodImplAttribute(MethodImplOptions.Synchronized)]
  237.             public override int Read(            [In(), Out()]
  238. char[] buffer, int index, int count)
  239.             {
  240.                 return _in.Read(buffer, index, count);
  241.             }
  242.            
  243.             [MethodImplAttribute(MethodImplOptions.Synchronized)]
  244.             public override int ReadBlock(            [In(), Out()]
  245. char[] buffer, int index, int count)
  246.             {
  247.                 return _in.ReadBlock(buffer, index, count);
  248.             }
  249.            
  250.             [MethodImplAttribute(MethodImplOptions.Synchronized)]
  251.             public override string ReadLine()
  252.             {
  253.                 return _in.ReadLine();
  254.             }
  255.            
  256.             [MethodImplAttribute(MethodImplOptions.Synchronized)]
  257.             public override string ReadToEnd()
  258.             {
  259.                 return _in.ReadToEnd();
  260.             }
  261.         }
  262.     }
  263. }

Developer Fusion