1

The Microsoft website has the code snippet:

 using (FileStream fsSource = new FileStream(pathSource, FileMode.Open, FileAccess.Read)) { // Read the source file into a byte array. byte[] bytes = new byte[fsSource.Length]; int numBytesToRead = (int)fsSource.Length; int numBytesRead = 0; while (numBytesToRead > 0) { // Read may return anything from 0 to numBytesToRead. int n = fsSource.Read(bytes, numBytesRead, numBytesToRead); // Break when the end of the file is reached. if (n == 0) break; numBytesRead += n; numBytesToRead -= n; } } 

What concerns me is that fsSource.Length is a long, whereas numBytesRead is an int so at most only 2 * int.MaxValue can be read into bytes (the head and the tail of the stream). So my questions are:

  1. Is there some reason that this is OK?
  2. If not, how should you read a FileStream into a byte[].
7
  • int.MaxValue is 2GB. If you haven't already given special consideration to the fact that you are proposing to load more than 4GB of data into memory all at once, please do so now. Commented Jun 17, 2011 at 9:14
  • 1
    @B Tyler, @Jon: The Microsoft CLR has a 2GB max object size limit, so you're never going to be able to put more than int.MaxValue bytes in there however you read it. (Other platforms, like Mono, might not have that restriction.) Commented Jun 17, 2011 at 9:19
  • @Jon - So that would come under 1. then? There's no need to be like that about it, it is a valid question; your's is a valid answer, it's just not phrased very nicely. Commented Jun 17, 2011 at 9:19
  • @B Tyler: It was just a factual comment. Apologies if it (unintentionally) sounded rude, although it doesn't read like that to me. Commented Jun 17, 2011 at 9:21
  • @LukeH - not you, I thought your comment was spot on, thanks: it was a race condition. Commented Jun 17, 2011 at 9:23

3 Answers 3

3

In this situation I wouldn't even bother processing the FileStream manually; use File.ReadAllBytes instead:

byte[] bytes = File.ReadAllBytes(pathSource); 
Sign up to request clarification or add additional context in comments.

2 Comments

One downside here is that if the file is really that long for the original question to be a highlighted concern, then this does not facilitate the programmer to implement 'message pumping' / cancellation etc from the executing thread, through a long blocking call.
@Mr. Disappointment: Very true. I was actually going to edit my answer to mention that, but got distracted. It's also possible in that situation that the OP might still want to read the entire stream, but buffer it into multiple byte[] arrays, or a composite collection of some kind.
3

To answer your question:

  1. The sample code is good for most of applications where we are not reaching extremes.
  2. If you have really long stream like say a video, use BufferedStream. Sample code is available at MSDN site

Comments

-1

Example using ReadAllBytes:

private byte[] m_cfgBuffer; m_cfgBuffer = File.ReadAllBytes(m_FileName); StringBuilder PartNbr = new StringBuilder(); StringBuilder Version = new StringBuilder(); int i, j; byte b; i = 356; // We know that the cfg file header ends at position 356 (1st hex(80)) b = m_cfgBuffer[i]; while (b != 0x80) // Scan for 2nd hex(80) { i++; b = m_cfgBuffer[i]; } // Now extract the part number - 6 bytes after hex(80) m_PartNbrPos = i + 5; for (j = m_PartNbrPos; j < m_PartNbrPos + 6; j++) { char cP = (char)m_cfgBuffer[j]; PartNbr.Append(cP); } m_PartNbr = PartNbr.ToString(); // Now, extract version number - 6 bytes after part number m_VersionPos = (m_PartNbrPos + 6) + 6; for (j = m_VersionPos; j < m_VersionPos + 2; j++) { char cP = (char)m_cfgBuffer[j]; Version.Append(cP); } m_Version = Version.ToString(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.