file_port.go 803 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //go:build windows || appengine || tinygo
  2. // +build windows appengine tinygo
  3. package msgp
  4. import (
  5. "io/ioutil"
  6. "os"
  7. )
  8. // MarshalSizer is the combination
  9. // of the Marshaler and Sizer
  10. // interfaces.
  11. type MarshalSizer interface {
  12. Marshaler
  13. Sizer
  14. }
  15. func ReadFile(dst Unmarshaler, file *os.File) error {
  16. if u, ok := dst.(Decodable); ok {
  17. return u.DecodeMsg(NewReader(file))
  18. }
  19. data, err := ioutil.ReadAll(file)
  20. if err != nil {
  21. return err
  22. }
  23. _, err = dst.UnmarshalMsg(data)
  24. return err
  25. }
  26. func WriteFile(src MarshalSizer, file *os.File) error {
  27. if e, ok := src.(Encodable); ok {
  28. w := NewWriter(file)
  29. err := e.EncodeMsg(w)
  30. if err == nil {
  31. err = w.Flush()
  32. }
  33. return err
  34. }
  35. raw, err := src.MarshalMsg(nil)
  36. if err != nil {
  37. return err
  38. }
  39. _, err = file.Write(raw)
  40. return err
  41. }