Click here to Skip to main content
15,887,596 members
Articles / Programming Languages / C#

BinaryReader.ReadLine extension

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
31 May 2015CPOL1 min read 14.3K   3  
This helper add a ReadLine method to the BinaryReader. This allows programs to read both binary and text from the same stream.

Introduction

The .NET framework has to distinct classes: BinaryReader reads bytes and binary whereas TextReader and StreamReader are reading texts.

In some situations you might want to switch from one to the other, typically if you want to read an FTP or HTTP streaml, you'll need to have the ability to read both ASCII and Binary from the the same stream.

Many people have requested it in forums. I have seen several attempts to achieved this but haven't seen anything simple that cover all encodings.  This is why I decided to post my solution.

This little function offer a simple (if not super fast) method to get a string (encoded in ASCII or any other encoding), from a BinaryReader.

 

Using the code

There is no DLL to add. Simply add the class below to your solution.

The code add a ReadLine() new method to the BinaryStream.

C#
    public static class BinaryReaderExtension
    {

        public static String ReadLine(this BinaryReader reader)
        {
            var result = new StringBuilder();
            bool foundEndOfLine = false;
            char ch;
            while (!foundEndOfLine)
            {
                try
                {
                    ch = reader.ReadChar();
                }
                catch (EndOfStreamException ex)
                {
                    if (result.Length == 0) return null;
                    else break;
                }
                
                switch (ch)
                {
                    case '\r':
                        if (reader.PeekChar() == '\n') reader.ReadChar();
                        foundEndOfLine = true;
                        break;
                    case '\n':
                        foundEndOfLine = true;
                        break;
                    default:
                        result.Append(ch);
                        break;
                }
            }
            return result.ToString();
        }
    }

 

Points of Interest

The code follows the same pattern than the StreamReader.ReadLine:

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.

History

2015-06-01 First vesion

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
France France
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

Comments and Discussions

 
-- There are no messages in this forum --