Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-detect binary or XML #17

Open
jpap opened this issue Sep 4, 2019 · 1 comment
Open

Auto-detect binary or XML #17

jpap opened this issue Sep 4, 2019 · 1 comment

Comments

@jpap
Copy link
Contributor

jpap commented Sep 4, 2019

Thanks for a great package!

I end up needing to have an external func to automatically decode a binary or XML plist, see below.

It would be nice to replace the deprecated plist.NewDecoder func with this. Happy to provide a PR on your suggestion.

// NewPListDecoder returns a new Property List decoder, auto-detecting
// whether it is binary or XML text.
func NewPListDecoder(r io.ReadSeeker) *plist.Decoder {
	// Detect if it XML or Binary
	magic := make([]byte, 5)
	n, err := r.Read(magic)
	if err != nil {
		return nil
	}
	_, err = r.Seek(0, 0)
	if err != nil {
		return nil
	}
	switch string(magic[:n]) {
	case "<?xml":
		return plist.NewXMLDecoder(r)
	case "bplis":
		return plist.NewBinaryDecoder(r)
	}
	return nil
}
@DHowett
Copy link

DHowett commented Jul 20, 2020

Incidentally, this is the exact reason the upstream package prefers a ReadSeeker 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants