I have a function here when the caller wants the number of bytes and then returns the bytes, but if there are not enough bytes in the file, it should return a smaller array. is there a better approach for this? I mean do not get 2 arrays 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;
}
}
user415789
source
share