Code:
                         / 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / IO / StringReader.cs / 1305376 / StringReader.cs
                        
                        
                            // ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
/*============================================================
** 
** Class:  StringReader 
**
** [....]  
**
** Purpose: For reading text from strings
**
** 
===========================================================*/
 
using System; 
using System.Runtime.InteropServices;
using System.Diagnostics.Contracts; 
namespace System.IO {
    // This class implements a text reader that reads from a string.
    // 
     [Serializable]
[System.Runtime.InteropServices.ComVisible(true)] 
    public class StringReader : TextReader 
    {
        private String _s; 
        private int _pos;
        private int _length;
        [System.Security.SecuritySafeCritical]  // auto-generated 
        public StringReader(String s) {
            if (s == null) 
                throw new ArgumentNullException("s"); 
            Contract.EndContractBlock();
            _s = s; 
            _length = s == null? 0: s.Length;
        }
        // Closes this StringReader. Following a call to this method, the String 
        // Reader will throw an ObjectDisposedException.
        public override void Close() { 
            Dispose(true); 
        }
 
        protected override void Dispose(bool disposing) {
            _s = null;
            _pos = 0;
            _length = 0; 
            base.Dispose(disposing);
        } 
 
        // Returns the next available character without actually reading it from
        // the underlying string. The current position of the StringReader is not 
        // changed by this operation. The returned value is -1 if no further
        // characters are available.
        //
        [Pure] 
        [System.Security.SecuritySafeCritical]  // auto-generated
        public override int Peek() { 
            if (_s == null) 
                __Error.ReaderClosed();
            if (_pos == _length) return -1; 
            return _s[_pos];
        }
        // Reads the next character from the underlying string. The returned value 
        // is -1 if no further characters are available.
        // 
        [System.Security.SecuritySafeCritical]  // auto-generated 
        public override int Read() {
            if (_s == null) 
                __Error.ReaderClosed();
            if (_pos == _length) return -1;
            return _s[_pos++];
        } 
        // Reads a block of characters. This method will read up to count 
        // characters from this StringReader into the buffer character 
        // array starting at position index. Returns the actual number of
        // characters read, or zero if the end of the string is reached. 
        //
        public override 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.EndContractBlock();
            if (_s == null)
                __Error.ReaderClosed(); 
            int n = _length - _pos; 
            if (n > 0) { 
                if (n > count) n = count;
                _s.CopyTo(_pos, buffer, index, n); 
                _pos += n;
            }
            return n;
        } 
        public override String ReadToEnd() 
        { 
            if (_s == null)
                __Error.ReaderClosed(); 
            String s;
            if (_pos==0)
                s = _s;
            else 
                s = _s.Substring(_pos, _length - _pos);
            _pos = _length; 
            return s; 
        }
 
        // 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 underlying string has been reached.
        // 
        [System.Security.SecuritySafeCritical]  // auto-generated 
        public override String ReadLine() {
            if (_s == null) 
                __Error.ReaderClosed();
            int i = _pos;
            while (i < _length) {
                char ch = _s[i]; 
                if (ch == '\r' || ch == '\n') {
                    String result = _s.Substring(_pos, i - _pos); 
                    _pos = i + 1; 
                    if (ch == '\r' && _pos < _length && _s[_pos] == '\n') _pos++;
                    return result; 
                }
                i++;
            }
            if (i > _pos) { 
                String result = _s.Substring(_pos, i - _pos);
                _pos = i; 
                return result; 
            }
            return null; 
        }
    }
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
/*============================================================
** 
** Class:  StringReader 
**
** [....]  
**
** Purpose: For reading text from strings
**
** 
===========================================================*/
 
using System; 
using System.Runtime.InteropServices;
using System.Diagnostics.Contracts; 
namespace System.IO {
    // This class implements a text reader that reads from a string.
    // 
     [Serializable]
[System.Runtime.InteropServices.ComVisible(true)] 
    public class StringReader : TextReader 
    {
        private String _s; 
        private int _pos;
        private int _length;
        [System.Security.SecuritySafeCritical]  // auto-generated 
        public StringReader(String s) {
            if (s == null) 
                throw new ArgumentNullException("s"); 
            Contract.EndContractBlock();
            _s = s; 
            _length = s == null? 0: s.Length;
        }
        // Closes this StringReader. Following a call to this method, the String 
        // Reader will throw an ObjectDisposedException.
        public override void Close() { 
            Dispose(true); 
        }
 
        protected override void Dispose(bool disposing) {
            _s = null;
            _pos = 0;
            _length = 0; 
            base.Dispose(disposing);
        } 
 
        // Returns the next available character without actually reading it from
        // the underlying string. The current position of the StringReader is not 
        // changed by this operation. The returned value is -1 if no further
        // characters are available.
        //
        [Pure] 
        [System.Security.SecuritySafeCritical]  // auto-generated
        public override int Peek() { 
            if (_s == null) 
                __Error.ReaderClosed();
            if (_pos == _length) return -1; 
            return _s[_pos];
        }
        // Reads the next character from the underlying string. The returned value 
        // is -1 if no further characters are available.
        // 
        [System.Security.SecuritySafeCritical]  // auto-generated 
        public override int Read() {
            if (_s == null) 
                __Error.ReaderClosed();
            if (_pos == _length) return -1;
            return _s[_pos++];
        } 
        // Reads a block of characters. This method will read up to count 
        // characters from this StringReader into the buffer character 
        // array starting at position index. Returns the actual number of
        // characters read, or zero if the end of the string is reached. 
        //
        public override 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.EndContractBlock();
            if (_s == null)
                __Error.ReaderClosed(); 
            int n = _length - _pos; 
            if (n > 0) { 
                if (n > count) n = count;
                _s.CopyTo(_pos, buffer, index, n); 
                _pos += n;
            }
            return n;
        } 
        public override String ReadToEnd() 
        { 
            if (_s == null)
                __Error.ReaderClosed(); 
            String s;
            if (_pos==0)
                s = _s;
            else 
                s = _s.Substring(_pos, _length - _pos);
            _pos = _length; 
            return s; 
        }
 
        // 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 underlying string has been reached.
        // 
        [System.Security.SecuritySafeCritical]  // auto-generated 
        public override String ReadLine() {
            if (_s == null) 
                __Error.ReaderClosed();
            int i = _pos;
            while (i < _length) {
                char ch = _s[i]; 
                if (ch == '\r' || ch == '\n') {
                    String result = _s.Substring(_pos, i - _pos); 
                    _pos = i + 1; 
                    if (ch == '\r' && _pos < _length && _s[_pos] == '\n') _pos++;
                    return result; 
                }
                i++;
            }
            if (i > _pos) { 
                String result = _s.Substring(_pos, i - _pos);
                _pos = i; 
                return result; 
            }
            return null; 
        }
    }
}
// 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
- SpecialFolderEnumConverter.cs
- QilScopedVisitor.cs
- SourceElementsCollection.cs
- SqlUtils.cs
- ClientBuildManagerTypeDescriptionProviderBridge.cs
- SystemBrushes.cs
- ConfigurationElement.cs
- DataSourceConverter.cs
- CodeBlockBuilder.cs
- DecimalConverter.cs
- TypedLocationWrapper.cs
- RegexNode.cs
- InputProviderSite.cs
- GridViewColumnHeaderAutomationPeer.cs
- EncodingTable.cs
- Point4D.cs
- OracleDataAdapter.cs
- FixedDSBuilder.cs
- AutoResizedEvent.cs
- ProcessThread.cs
- ExeContext.cs
- XsltOutput.cs
- MarkupCompilePass1.cs
- GeometryGroup.cs
- AssemblyInfo.cs
- CanonicalFontFamilyReference.cs
- EntityDataSourceDataSelectionPanel.designer.cs
- PerformanceCounterCategory.cs
- FilterQuery.cs
- DataStreams.cs
- XmlObjectSerializer.cs
- RowToFieldTransformer.cs
- StreamSecurityUpgradeAcceptorBase.cs
- DbDataSourceEnumerator.cs
- CodeExpressionCollection.cs
- control.ime.cs
- RequestQueue.cs
- StreamResourceInfo.cs
- DurableEnlistmentState.cs
- LicenseProviderAttribute.cs
- SqlDataSourceEnumerator.cs
- RegisteredExpandoAttribute.cs
- StylusDevice.cs
- OraclePermissionAttribute.cs
- QueryReaderSettings.cs
- XmlDictionaryReader.cs
- RelationshipWrapper.cs
- MessagePropertyFilter.cs
- TcpProcessProtocolHandler.cs
- BehaviorService.cs
- WebPartHeaderCloseVerb.cs
- InstanceCreationEditor.cs
- SerialPinChanges.cs
- ObjectDataSourceEventArgs.cs
- SamlAuthorizationDecisionStatement.cs
- Model3DCollection.cs
- UnsafeNativeMethods.cs
- IPAddressCollection.cs
- AttributeCollection.cs
- XmlSerializerSection.cs
- DataGridTextBoxColumn.cs
- ObjectListCommandEventArgs.cs
- messageonlyhwndwrapper.cs
- MouseBinding.cs
- ExtendLockAsyncResult.cs
- XmlSiteMapProvider.cs
- HandledEventArgs.cs
- DelegatingHeader.cs
- DrawListViewItemEventArgs.cs
- DataRelation.cs
- Attributes.cs
- DbProviderSpecificTypePropertyAttribute.cs
- SocketManager.cs
- DBCSCodePageEncoding.cs
- fixedPageContentExtractor.cs
- XmlComment.cs
- EncryptedPackageFilter.cs
- EntitySetRetriever.cs
- WriteTimeStream.cs
- SuppressIldasmAttribute.cs
- StylusPoint.cs
- WebConfigurationHostFileChange.cs
- WindowsRichEdit.cs
- CodeTypeReference.cs
- JournalEntryStack.cs
- ResourceManagerWrapper.cs
- DropDownList.cs
- OleDbConnection.cs
- DiagnosticsConfiguration.cs
- TextModifierScope.cs
- TimeZone.cs
- TypeListConverter.cs
- Stackframe.cs
- RegisteredArrayDeclaration.cs
- DesignerValidatorAdapter.cs
- SoapException.cs
- SqlErrorCollection.cs
- PeerInvitationResponse.cs
- BamlRecordWriter.cs
- IChannel.cs