Search Results for

    Show / Hide Table of Contents

    Class StreamReader

    Implements a TextReader that reads characters from a byte stream in a particular encoding.

    Inheritance
    object
    MarshalByRefObject
    TextReader
    StreamReader
    Implements
    IDisposable
    Inherited Members
    TextReader.Dispose()
    TextReader.ReadBlock(char[], int, int)
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    Namespace: System.IO
    Assembly: System.IO.Streams.dll
    Syntax
    public class StreamReader : TextReader, IDisposable

    Constructors

    StreamReader(Stream)

    Initializes a new instance of the StreamReader class for the specified stream.

    Declaration
    public StreamReader(Stream stream)
    Parameters
    Type Name Description
    Stream stream

    The stream to be read.

    Exceptions
    Type Condition
    ArgumentNullException

    stream is null.

    ArgumentException

    stream does not support reading.

    Properties

    BaseStream

    Returns the underlying stream.

    Declaration
    public virtual Stream BaseStream { get; }
    Property Value
    Type Description
    Stream

    The underlying stream.

    Remarks

    You use this property to access the underlying stream. The StreamReader class buffers input from the underlying stream when you call one of the Read methods. If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer. To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary. The StreamReader constructors that have the detectEncodingFromByteOrderMarks parameter can change the encoding the first time you read from the StreamReader object.

    CurrentEncoding

    Gets the current character encoding that the current StreamReader object is using.

    Declaration
    public virtual Encoding CurrentEncoding { get; }
    Property Value
    Type Description
    Encoding

    The current character encoding used by the current reader. The value can be different after the first call to any Read() method of StreamReader, since encoding autodetection is not done until the first call to a Read() method.

    EndOfStream

    Gets a value that indicates whether the current stream position is at the end of the stream.

    Declaration
    public bool EndOfStream { get; }
    Property Value
    Type Description
    bool

    true if the current stream position is at the end of the stream; otherwise false.

    Methods

    Close()

    Closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader.

    Declaration
    public override void Close()
    Overrides
    TextReader.Close()
    Remarks

    This method overrides the Close() method. This implementation of Close() calls the Dispose(bool) method, passing a true value. Following a call to Close(), any operations on the reader might raise exceptions.

    Dispose(bool)

    Closes the underlying stream, releases the unmanaged resources used by the StreamReader, and optionally releases the managed resources.

    Declaration
    protected override void Dispose(bool disposing)
    Parameters
    Type Name Description
    bool disposing

    true to release both managed and unmanaged resources; false to release only unmanaged resources.

    Overrides
    TextReader.Dispose(bool)
    Remarks

    This method is called by the public Dispose(bool) method and the Finalize method. Dispose invokes the protected Dispose(bool) method with the disposing parameter set to true. Finalize invokes Dispose(bool) with disposing set to false. When the disposing parameter is true, this method releases all resources held by any managed objects that the StreamReader object references.This method invokes the Dispose(bool) method of each referenced object.

    Peek()

    Returns the next available character but does not consume it.

    Declaration
    public override int Peek()
    Returns
    Type Description
    int

    An integer representing the next character to be read, or -1 if there are no characters to be read or if the stream does not support seeking.

    Overrides
    TextReader.Peek()
    Remarks

    The Peek() method returns an integer value in order to determine whether the end of the file, or another error has occurred. This allows a user to first check if the returned value is -1 before casting it to a char type. This method overrides Peek(). The current position of the StreamReader object is not changed by Peek().

    Exceptions
    Type Condition
    IOException

    An I/O error occurs.

    Read()

    Reads the next character from the input stream and advances the character position by one character.

    Declaration
    public override int Read()
    Returns
    Type Description
    int

    The next character from the input stream represented as an int object, or -1 if no more characters are available.

    Overrides
    TextReader.Read()
    Remarks

    This method overrides Read(). This method returns an integer so that it can return -1 if the end of the stream has been reached. If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer.To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary.

    Read(char[], int, int)

    Reads a specified maximum of characters from the current stream into a buffer, beginning at the specified index.

    Declaration
    public override int Read(char[] buffer, int index, int count)
    Parameters
    Type Name Description
    char[] buffer

    When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.

    int index

    The index of buffer at which to begin writing.

    int count

    The maximum number of characters to read.

    Returns
    Type Description
    int

    The number of characters that have been read, or 0 if at the end of the stream and no data was read. The number will be less than or equal to the count parameter, depending on whether the data is available within the stream.

    Overrides
    TextReader.Read(char[], int, int)
    Remarks

    This method overrides TextReader.Read. This method returns an integer so that it can return 0 if the end of the stream has been reached. When using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream, where the internal buffer is set to your desired block size, and to always read less than the block size.If the size of the internal buffer was unspecified when the stream was constructed, its default size is 4 kilobytes(4096 bytes). If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer.To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary. This method returns after either the number of characters specified by the count parameter are read, or the end of the file is reached. ReadBlock(char[], int, int) is a blocking version of Read(char[], int, int).

    Exceptions
    Type Condition
    ArgumentNullException

    buffer is null.

    ArgumentOutOfRangeException

    index or count is negative.

    ArgumentException

    The buffer length minus index is less than count.

    ObjectDisposedException

    An I/O error occurs, such as the stream is closed.

    ReadLine()

    Reads a line of characters from the current stream and returns the data as a string.

    Declaration
    public override string ReadLine()
    Returns
    Type Description
    string

    The next line from the input stream, or null if the end of the input stream is reached.

    Overrides
    TextReader.ReadLine()
    Exceptions
    Type Condition
    Exception

    ReadToEnd()

    Reads all characters from the current position to the end of the stream.

    Declaration
    public override string ReadToEnd()
    Returns
    Type Description
    string

    The rest of the stream as a string, from the current position to the end. If the current position is at the end of the stream, returns an empty string ("").

    Overrides
    TextReader.ReadToEnd()
    Remarks

    This method overrides TextReader.ReadToEnd. ReadToEnd works best when you need to read all the input from the current position to the end of the stream.If more control is needed over how many characters are read from the stream, use the Read(Char[], Int32, Int32) method overload, which generally results in better performance. ReadToEnd assumes that the stream knows when it has reached an end.For interactive protocols in which the server sends data only when you ask for it and does not close the connection, ReadToEnd might block indefinitely because it does not reach an end, and should be avoided. Note that when using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream.If the size of the buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes). If the current method throws an OutOfMemoryException, the reader's position in the underlying Stream object is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadLine buffer are discarded. If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer. To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary.

    Implements

    IDisposable

    Extension Methods

    LogDispatcher.GetCurrentClassLogger(object)
    LogDispatcher.GetCurrentClassLogger(object)
    LogDispatcher.GetCurrentClassLogger(object)
    LogDispatcher.GetCurrentClassLogger(object)
    LogDispatcher.GetCurrentClassLogger(object)
    LogDispatcher.GetCurrentClassLogger(object)
    LogDispatcher.GetCurrentClassLogger(object)
    LogDispatcher.GetCurrentClassLogger(object)
    LogDispatcher.GetCurrentClassLogger(object)
    LogDispatcher.GetCurrentClassLogger(object)
    In This Article
    Back to top Copyright © 2023 nanoFramework Contributors
    Generated by DocFX