1

Two related questions:

  1. Why did they choose to return string from StreamReader.ReadToEnd() instead of byte[]?
  2. I know I can convert it using System.Text.Encoding, but exactly which encoding should I use? UTF8, UTF7, ASCII or Unicode? I'm dealing with binary files.

Note: I was asked for clarification. It is just that the word stream gives an impression of byte-level...well...stream of data (think network stream, file stream, buffer, none of them is supposed to be a char-type concept). As correctly indicated by Joel in his answer, this is more of a naming issue than anything else.

2
  • Very unclear why you suggest that TextReader should not return strings. Some reasoning would be welcome update to the question. Commented Mar 29, 2017 at 5:02
  • @AlexeiLevenkov: Added more clarification. Commented Mar 29, 2017 at 7:19

2 Answers 2

5

"Why" questions are rarely good fodder for Stack Overflow, since the only ones who can really answer them are those on the design committee for the product.

However, in this case we can at least give you a good indication by pointing you to the StreamReader documentation, where you'll find this:

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

So we see, then, that working with text rather than bytes is what StreamReader is all about. Maybe you would ask why it's named "StreamReader" rather than "TextStreamReader", but then I'd have to refer you back to my first sentence.

As for getting a byte array... it depends on what you want to do. .Net uses Unicode for it's strings, but the underlying stream might be something different, and whatever you plan to do with this data might demand a different encoding entirely.

In this case, though, you have binary files, and you want a Byte[]. Rather than messing with streams at all, just look at File.ReadAllBytes(). If you have to deal with streams you might also want a BinaryReader.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm... Insightful. I can't use ReadAllBytes because I have to read from a particular offset, but BinaryReader is a good alternate. It is just that the name itself is a bit misleading.
@dotNET Yep, we program to just the name. But, every section of the documentation can be valuable. Sometimes, it's the namespace, sometimes the assembly, sometimes even the remarks hold the key information. In this case, it's the inheritance hierarchy.
1

You have a number of alternatives to StreamReader, which is used mostly for reading in character data. If you are reading in binary file data, then a better option is to use FileStream.Read. It returns the data in a byte array.

According to my benchmarks, it is faster than BinaryReader and File.ReadAllBytes. But, File.ReadAllBytes is easier to use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.