remko / go-mkvparse

Fast Matroska parser in Go
MIT License
34 stars 11 forks source link

Change handler interface for lazy loading of unknown-sized elements #6

Open remko opened 4 years ago

remko commented 4 years ago

Change the following methods on the Handler interface:

HandleBinary(ElementID, []byte, ElementInfo) error
HandleString(ElementID, string, ElementInfo) error

to

HandleBinary(ElementID, io.Reader, ElementInfo) error
HandleString(ElementID, io.Reader, ElementInfo) error

This allows handlers to only read part of the data (in case it is too much for them to process). Alternatively, change the signature to

HandleBytes(ElementID, BytesType, io.Reader, ElementInfo)

where BytesType is String or Binary.

The following line needs to be added to all existing handlers to fix compilation for binary cases.

HandleBinary(id ElementID, r io.Reader, info ElementInfo) error {
  data, _ := ioutil.ReadAll(r)
  // use data as before. should be safe to ignore errors (the parser will handle these)
}

To make it more convenient, consider adding a convenience function:

HandleBinary(id ElementID, r io.Reader, info ElementInfo) error {
  data := mkvparse.ReadBytes(r)
  // use data as before
}

Because reading strings require more code, a convenience function for strings is useful:

HandleString(id ElementID, r io.Reader, info ElementInfo) error {
  str := mkvparse.ReadString(r)
  // use str as before
}

The Read* functions probably need a parameter to limit the size of the read (with -1 to mean 'Unlimited')

Another alternative is to add an UnsafeHandler interface, and adapt it to the new handler interface:

ParsePath("example.mkv", NewUnsafeHandler(unsafeHandler))