Create Binary stream of file

Blessyjees
Posted by Blessyjees under C# category on | Points: 40 | Views : 2201
This code will give the binsry strem of the given file path
FileStream fileStream;
byte[] fileByte;
using (fileStream = File.OpenRead(filePath))
{
fileByte = new byte[fileStream.Length];
ReadWholeArray(fileStream, fileByte);
}
return fileByte;

private static void ReadWholeArray(Stream stream, byte[] data)
{
int offset = 0;
int remaining = data.Length;
while (remaining > 0)
{
int read = stream.Read(data, offset, remaining);
if (read <= 0)
{
throw new EndOfStreamException(String.Format("{0}:End of stream reached with {1} bytes left to read", "Generate binary stream", remaining));
}

remaining -= read;
offset += read;
}
}


Comments or Responses

Posted by: T.saravanan on: 10/12/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi,

In your code what is the use of ReadWholeArray method kindly give a little bit explanation about your code.Its really useful to viewers.
Posted by: Blessyjees on: 10/17/2011 Level:Bronze | Status: [Member] | Points: 10
hi,

sorry, I missed to include the code of the function ReadWholeArray. I have updated the code. This function is used to read the stream as different group instead of full stream at a time.
Posted by: T.saravanan on: 10/17/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Thanks....

Login to post response