Skip to content

Observed Binary File

The ObservedBinaryFile class within the ByteCobra.Observables namespace represents an observed binary file that clients can subscribe to for change notifications. The class utilizes the FileSystemWatcher to monitor the specified file for changes, deletions, creations, and renaming events, and notifies subscribed clients accordingly.

The ObservedBinaryFile class encapsulates a FileSystemWatcher instance to monitor the specified binary file for any changes, deletions, creations, or renaming events. When any of these events occur, the corresponding event handler (OnBinaryFileChanged, OnBinaryFileDeleted, or OnBinaryFileRenamed) is invoked. These handlers then call the NotifySubscribers method to notify all subscribed clients about the change, passing along the new file contents and the type of change.

The Subscribe, Unsubscribe, and ClearSubscriptions methods manage a list of subscribers, which are client callback methods to be invoked when the observed binary file changes. The ReadAllBytes, WriteAllBytes, and AppendBytes methods provide read and write access to the file contents, with WriteAllBytes and AppendBytes also triggering change notifications to subscribed clients.

This class is designed to provide a robust mechanism for monitoring binary file changes in a decoupled and type-safe manner, facilitating a variety of scenarios where clients need to react to changes in file data.

Properties

  • string Path: Gets the path to the observed binary file.
  • bool HandleExceptions: Gets or sets whether exceptions should be handled when notifying subscribers.

Methods

  • ObservedBinaryFile(string path): Constructor that initializes a new instance of the ObservedBinaryFile class with the specified path.
  • void Subscribe(Action<byte[], WatcherChangeTypes> callback): Subscribes a callback method to be called when the observed file changes.
  • void Unsubscribe(Action<byte[], WatcherChangeTypes> callback): Unsubscribes a callback method from receiving change notifications.
  • void ClearSubscriptions(): Clears all subscriptions to this observed binary file.
  • byte[] ReadAllBytes(): Reads all bytes from the observed binary file.
  • void Move(string path): Moves the observed binary file to a new path.
  • void WriteAllBytes(byte[] bytes): Writes all bytes to the observed binary file, overwriting its contents.
  • void AppendBytes(byte[] bytes): Appends bytes to the observed binary file.
  • protected virtual void NotifySubscribers(byte[] bytes, WatcherChangeTypes type): Notifies all subscribers about a change in the observed binary file.

Event Handling Methods

  • protected virtual void OnBinaryFileChanged(object sender, FileSystemEventArgs e): Handles the file changed event.
  • protected virtual void OnBinaryFileDeleted(object sender, FileSystemEventArgs e): Handles the file deleted event.
  • protected void OnBinaryFileRenamed(object sender, RenamedEventArgs e): Handles the file renamed event.

Example

var observedFile = new ObservedBinaryFile("example.bin");

observedFile.Subscribe((bytes, changeType) =>
{
    Console.WriteLine($"File changed with type: {changeType}");
});

observedFile.WriteAllBytes(new byte[] { 0x01, 0x02, 0x03 });
// Output: File changed with type: Created

observedFile.AppendBytes(new byte[] { 0x04, 0x05, 0x06 });
// Output: File changed with type: Changed