You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
930 B

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package binary
  2. import "io"
  3. type String string
  4. // String
  5. func (self String) Equals(other Binary) bool {
  6. return self == other
  7. }
  8. func (self String) Less(other Binary) bool {
  9. if o, ok := other.(String); ok {
  10. return self < o
  11. } else {
  12. panic("Cannot compare unequal types")
  13. }
  14. }
  15. func (self String) ByteSize() int {
  16. return len(self) + 4
  17. }
  18. func (self String) WriteTo(w io.Writer) (n int64, err error) {
  19. var n_ int
  20. _, err = UInt32(len(self)).WriteTo(w)
  21. if err != nil {
  22. return n, err
  23. }
  24. n_, err = w.Write([]byte(self))
  25. return int64(n_ + 4), err
  26. }
  27. func ReadStringSafe(r io.Reader) (String, error) {
  28. length, err := ReadUInt32Safe(r)
  29. if err != nil {
  30. return "", err
  31. }
  32. bytes := make([]byte, int(length))
  33. _, err = io.ReadFull(r, bytes)
  34. if err != nil {
  35. return "", err
  36. }
  37. return String(bytes), nil
  38. }
  39. func ReadString(r io.Reader) String {
  40. str, err := ReadStringSafe(r)
  41. if r != nil {
  42. panic(err)
  43. }
  44. return str
  45. }