I just have a function here that caller wants number of bytes and then it returns the bytes but if there are not enough bytes in file it should return an smaller array. is there better approach to do this? I mean not getting 2 array and use BlockCopy?
byte[] GetPartialPackage(string filePath, long offset, int count)
{
using (var reader = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
reader.Seek(offset, SeekOrigin.Begin);
byte[] tempData = new byte[count];
int num = reader.Read(tempData, 0, count);
byte[] tempdata = new byte[num];
Buffer.BlockCopy(tempData, 0, tempdata, 0, num);
return tempdata;
}
}
Just update the count based on the length of the stream to shorten it if necessary.