Is opening a file stream a costly operation
I'd like to provide a lazy loading functionality in a class that reads a structured file. Every element of the file has a header and a payload.
the idea is to load only the headers from the file and access the payload data only when the relevent field is accessed
a prototype of the class would look like this in C#
public class OneElement
{
public Header TheHeader {get; private set;}
private string _FileName;
private long _StreamPosition; // this value is initialized when the header is read
private Payload _ThePayload;
public Payload ThePayload
{
get{
if (_ThePayload == null)
using (var stream = File.OpenRead(_FileName) )
{
stream.seek(_StreamPosition,SeekOrigin.Begin); // seek to the relevent position
_ThePayload = ReadPayload(stream); // this method return the paylod read reads from the current position
}
return _ThePayload;
}
}
}
Will the operation of opening the file and getting the payload be costly especially in a context where the payload will represent audio or video data