mardi 16 novembre 2021

Guidelines for designing a robust file format writer?

Suppose you want to write a .WAV file format writer like so:

using var stream = File.OpenRead("test.wav");
using var writer = new WavWriter(stream, ... /* .WAV format parameters */);

// write the file

// writer.Dispose() does a few things:
// - writes user-added chunks
// - updates the file header (chunk size) so the file is valid

There is a concpetual problem in doing so:

  • the user can change the stream position and therefore screw the writing process

You may suggest the following:

  • the writer should own the stream, this would work if writing to a file, but not to a stream
  • own its own memory stream so it can write to streams too, okay but memory concerns

I guess you get the point...

To me, the only viable thing would be to document that aspect but I may have missed something, hence the question.

Question:

How to make a file format writer be able to write to a stream yet defend yourself about possible changes to its position?

Aucun commentaire:

Enregistrer un commentaire