Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / IO / TextReader.cs / 1305376 / TextReader.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== /*============================================================ ** ** Class: TextReader ** **[....] ** ** ** Purpose: Abstract base class for all Text-only Readers. ** Subclasses will include StreamReader & StringReader. ** ** ===========================================================*/ using System; using System.Text; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Reflection; using System.Security.Permissions; using System.Diagnostics.Contracts; namespace System.IO { // This abstract base class represents a reader that can read a sequential // stream of characters. This is not intended for reading bytes - // there are methods on the Stream class to read bytes. // A subclass must minimally implement the Peek() and Read() methods. // // This class is intended for character input, not bytes. // There are methods on the Stream class for reading bytes. [Serializable] [ComVisible(true)] #if FEATURE_REMOTING public abstract class TextReader : MarshalByRefObject, IDisposable { #if false } #endif // false #else // FEATURE_REMOTING public abstract class TextReader : IDisposable { #endif // FEATURE_REMOTING public static readonly TextReader Null = new NullTextReader(); protected TextReader() {} // Closes this TextReader and releases any system resources associated with the // TextReader. Following a call to Close, any operations on the TextReader // may raise exceptions. // // This default method is empty, but descendant classes can override the // method to provide the appropriate functionality. public virtual void Close() { Dispose(true); GC.SuppressFinalize(this); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { } // Returns the next available character without actually reading it from // the input stream. The current position of the TextReader is not changed by // this operation. The returned value is -1 if no further characters are // available. // // This default method simply returns -1. // [Pure] public virtual int Peek() { Contract.Ensures(Contract.Result() >= -1); return -1; } // Reads the next character from the input stream. The returned value is // -1 if no further characters are available. // // This default method simply returns -1. // public virtual int Read() { Contract.Ensures(Contract.Result () >= -1); return -1; } // Reads a block of characters. This method will read up to // count characters from this TextReader into the // buffer character array starting at position // index. Returns the actual number of characters read. // public virtual int Read([In, Out] char[] buffer, int index, int count) { if (buffer==null) throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer")); if (index < 0) throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (count < 0) throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (buffer.Length - index < count) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); Contract.Ensures(Contract.Result () >= 0); Contract.Ensures(Contract.Result () <= Contract.OldValue(count)); Contract.EndContractBlock(); int n = 0; do { int ch = Read(); if (ch == -1) break; buffer[index + n++] = (char)ch; } while (n < count); return n; } // Reads all characters from the current position to the end of the // TextReader, and returns them as one string. public virtual String ReadToEnd() { Contract.Ensures(Contract.Result () != null); char[] chars = new char[4096]; int len; StringBuilder sb = new StringBuilder(4096); while((len=Read(chars, 0, chars.Length)) != 0) { sb.Append(chars, 0, len); } return sb.ToString(); } // Blocking version of read. Returns only when count // characters have been read or the end of the file was reached. // public virtual int ReadBlock([In, Out] char[] buffer, int index, int count) { Contract.Ensures(Contract.Result () >= 0); Contract.Ensures(Contract.Result () <= count); int i, n = 0; do { n += (i = Read(buffer, index + n, count - n)); } while (i > 0 && n < count); return n; } // Reads a line. A line is defined as a sequence of characters followed by // a carriage return ('\r'), a line feed ('\n'), or a carriage return // immediately followed by a line feed. The resulting string does not // contain the terminating carriage return and/or line feed. The returned // value is null if the end of the input stream has been reached. // public virtual String ReadLine() { StringBuilder sb = new StringBuilder(); while (true) { int ch = Read(); if (ch == -1) break; if (ch == '\r' || ch == '\n') { if (ch == '\r' && Peek() == '\n') Read(); return sb.ToString(); } sb.Append((char)ch); } if (sb.Length > 0) return sb.ToString(); return null; } [HostProtection(Synchronization=true)] public static TextReader Synchronized(TextReader reader) { if (reader==null) throw new ArgumentNullException("reader"); Contract.Ensures(Contract.Result () != null); Contract.EndContractBlock(); if (reader is SyncTextReader) return reader; return new SyncTextReader(reader); } [Serializable] private sealed class NullTextReader : TextReader { public NullTextReader(){} public override int Read(char[] buffer, int index, int count) { return 0; } public override String ReadLine() { return null; } } [Serializable] internal sealed class SyncTextReader : TextReader { internal TextReader _in; internal SyncTextReader(TextReader t) { _in = t; } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override void Close() { // So that any overriden Close() gets run _in.Close(); } [MethodImplAttribute(MethodImplOptions.Synchronized)] protected override void Dispose(bool disposing) { // Explicitly pick up a potentially methodimpl'ed Dispose if (disposing) ((IDisposable)_in).Dispose(); } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override int Peek() { return _in.Peek(); } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override int Read() { return _in.Read(); } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override int Read([In, Out] char[] buffer, int index, int count) { return _in.Read(buffer, index, count); } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override int ReadBlock([In, Out] char[] buffer, int index, int count) { return _in.ReadBlock(buffer, index, count); } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override String ReadLine() { return _in.ReadLine(); } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override String ReadToEnd() { return _in.ReadToEnd(); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== /*============================================================ ** ** Class: TextReader ** ** [....] ** ** ** Purpose: Abstract base class for all Text-only Readers. ** Subclasses will include StreamReader & StringReader. ** ** ===========================================================*/ using System; using System.Text; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Reflection; using System.Security.Permissions; using System.Diagnostics.Contracts; namespace System.IO { // This abstract base class represents a reader that can read a sequential // stream of characters. This is not intended for reading bytes - // there are methods on the Stream class to read bytes. // A subclass must minimally implement the Peek() and Read() methods. // // This class is intended for character input, not bytes. // There are methods on the Stream class for reading bytes. [Serializable] [ComVisible(true)] #if FEATURE_REMOTING public abstract class TextReader : MarshalByRefObject, IDisposable { #if false } #endif // false #else // FEATURE_REMOTING public abstract class TextReader : IDisposable { #endif // FEATURE_REMOTING public static readonly TextReader Null = new NullTextReader(); protected TextReader() {} // Closes this TextReader and releases any system resources associated with the // TextReader. Following a call to Close, any operations on the TextReader // may raise exceptions. // // This default method is empty, but descendant classes can override the // method to provide the appropriate functionality. public virtual void Close() { Dispose(true); GC.SuppressFinalize(this); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { } // Returns the next available character without actually reading it from // the input stream. The current position of the TextReader is not changed by // this operation. The returned value is -1 if no further characters are // available. // // This default method simply returns -1. // [Pure] public virtual int Peek() { Contract.Ensures(Contract.Result() >= -1); return -1; } // Reads the next character from the input stream. The returned value is // -1 if no further characters are available. // // This default method simply returns -1. // public virtual int Read() { Contract.Ensures(Contract.Result () >= -1); return -1; } // Reads a block of characters. This method will read up to // count characters from this TextReader into the // buffer character array starting at position // index. Returns the actual number of characters read. // public virtual int Read([In, Out] char[] buffer, int index, int count) { if (buffer==null) throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer")); if (index < 0) throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (count < 0) throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (buffer.Length - index < count) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); Contract.Ensures(Contract.Result () >= 0); Contract.Ensures(Contract.Result () <= Contract.OldValue(count)); Contract.EndContractBlock(); int n = 0; do { int ch = Read(); if (ch == -1) break; buffer[index + n++] = (char)ch; } while (n < count); return n; } // Reads all characters from the current position to the end of the // TextReader, and returns them as one string. public virtual String ReadToEnd() { Contract.Ensures(Contract.Result () != null); char[] chars = new char[4096]; int len; StringBuilder sb = new StringBuilder(4096); while((len=Read(chars, 0, chars.Length)) != 0) { sb.Append(chars, 0, len); } return sb.ToString(); } // Blocking version of read. Returns only when count // characters have been read or the end of the file was reached. // public virtual int ReadBlock([In, Out] char[] buffer, int index, int count) { Contract.Ensures(Contract.Result () >= 0); Contract.Ensures(Contract.Result () <= count); int i, n = 0; do { n += (i = Read(buffer, index + n, count - n)); } while (i > 0 && n < count); return n; } // Reads a line. A line is defined as a sequence of characters followed by // a carriage return ('\r'), a line feed ('\n'), or a carriage return // immediately followed by a line feed. The resulting string does not // contain the terminating carriage return and/or line feed. The returned // value is null if the end of the input stream has been reached. // public virtual String ReadLine() { StringBuilder sb = new StringBuilder(); while (true) { int ch = Read(); if (ch == -1) break; if (ch == '\r' || ch == '\n') { if (ch == '\r' && Peek() == '\n') Read(); return sb.ToString(); } sb.Append((char)ch); } if (sb.Length > 0) return sb.ToString(); return null; } [HostProtection(Synchronization=true)] public static TextReader Synchronized(TextReader reader) { if (reader==null) throw new ArgumentNullException("reader"); Contract.Ensures(Contract.Result () != null); Contract.EndContractBlock(); if (reader is SyncTextReader) return reader; return new SyncTextReader(reader); } [Serializable] private sealed class NullTextReader : TextReader { public NullTextReader(){} public override int Read(char[] buffer, int index, int count) { return 0; } public override String ReadLine() { return null; } } [Serializable] internal sealed class SyncTextReader : TextReader { internal TextReader _in; internal SyncTextReader(TextReader t) { _in = t; } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override void Close() { // So that any overriden Close() gets run _in.Close(); } [MethodImplAttribute(MethodImplOptions.Synchronized)] protected override void Dispose(bool disposing) { // Explicitly pick up a potentially methodimpl'ed Dispose if (disposing) ((IDisposable)_in).Dispose(); } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override int Peek() { return _in.Peek(); } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override int Read() { return _in.Read(); } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override int Read([In, Out] char[] buffer, int index, int count) { return _in.Read(buffer, index, count); } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override int ReadBlock([In, Out] char[] buffer, int index, int count) { return _in.ReadBlock(buffer, index, count); } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override String ReadLine() { return _in.ReadLine(); } [MethodImplAttribute(MethodImplOptions.Synchronized)] public override String ReadToEnd() { return _in.ReadToEnd(); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ResourceContainer.cs
- AssemblyAssociatedContentFileAttribute.cs
- ScriptMethodAttribute.cs
- HotCommands.cs
- SkinBuilder.cs
- ModuleBuilderData.cs
- XmlTextWriter.cs
- DataGridViewAccessibleObject.cs
- WrappedReader.cs
- PointHitTestResult.cs
- ImageAutomationPeer.cs
- XsltException.cs
- OdbcErrorCollection.cs
- UIntPtr.cs
- TabControlCancelEvent.cs
- SystemIcons.cs
- AggregateNode.cs
- ScriptComponentDescriptor.cs
- OuterGlowBitmapEffect.cs
- StringToken.cs
- XPathDocument.cs
- InkCanvasFeedbackAdorner.cs
- TemplateEditingService.cs
- ApplicationDirectory.cs
- FileAuthorizationModule.cs
- FormViewDeleteEventArgs.cs
- RevocationPoint.cs
- OleDbCommandBuilder.cs
- GridProviderWrapper.cs
- HeaderCollection.cs
- SqlBuilder.cs
- GeneralTransform3DTo2DTo3D.cs
- ScrollBar.cs
- HiddenField.cs
- ObjectHandle.cs
- TextElementEnumerator.cs
- CacheMemory.cs
- RoutedUICommand.cs
- BaseTemplateParser.cs
- ToolStripItemImageRenderEventArgs.cs
- ByValueEqualityComparer.cs
- InstanceKeyCollisionException.cs
- OutputCacheSettingsSection.cs
- SqlFactory.cs
- XmlSchemaValidator.cs
- HttpStreamXmlDictionaryWriter.cs
- InputDevice.cs
- QueryStringConverter.cs
- NaturalLanguageHyphenator.cs
- QilStrConcat.cs
- FixedHyperLink.cs
- DateBoldEvent.cs
- IRCollection.cs
- PerformanceCounterCategory.cs
- DynamicILGenerator.cs
- CodeExpressionCollection.cs
- CodePrimitiveExpression.cs
- Point.cs
- ProviderConnectionPoint.cs
- PersonalizationState.cs
- ReaderWriterLockWrapper.cs
- ScrollBar.cs
- SimpleApplicationHost.cs
- SmiEventSink.cs
- ValidationSummary.cs
- ellipse.cs
- CodePrimitiveExpression.cs
- WebBaseEventKeyComparer.cs
- RoleBoolean.cs
- MissingFieldException.cs
- HttpListener.cs
- GCHandleCookieTable.cs
- XmlReader.cs
- AccessViolationException.cs
- WebRequestModuleElement.cs
- PropertyDescriptorCollection.cs
- KnownBoxes.cs
- TreeNodeMouseHoverEvent.cs
- NetworkCredential.cs
- ProfileManager.cs
- Underline.cs
- CustomErrorCollection.cs
- GuidelineCollection.cs
- BamlResourceDeserializer.cs
- ObjectDataSourceFilteringEventArgs.cs
- CustomAttributeFormatException.cs
- SyndicationDeserializer.cs
- HwndSourceParameters.cs
- ConstraintEnumerator.cs
- AssemblyNameProxy.cs
- XmlDocumentFragment.cs
- RootContext.cs
- WebBrowserProgressChangedEventHandler.cs
- TemplateInstanceAttribute.cs
- AmbientValueAttribute.cs
- ListView.cs
- ScriptingScriptResourceHandlerSection.cs
- ModifyActivitiesPropertyDescriptor.cs
- RenderDataDrawingContext.cs
- InternalException.cs