Created
May 13, 2014 17:54
-
-
Save nramsbottom/be1e80c37fc7c8658a8b to your computer and use it in GitHub Desktop.
StreamReader.ReadToEnd with maximum length
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static string ReadToEnd(this StreamReader reader, int maximumCharacters) | |
| { | |
| var builder = new System.Text.StringBuilder(); | |
| while (true) | |
| { | |
| var line = reader.ReadLine(); | |
| if (line == null) | |
| break; | |
| if (builder.Length + line.Length > maximumCharacters) | |
| { | |
| line = line.Substring(0, maximumCharacters - builder.Length - Environment.NewLine.Length); | |
| builder.AppendLine(line); | |
| break; | |
| } | |
| builder.AppendLine(line); | |
| } | |
| return builder.ToString(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment