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.

57 lines
1.0 KiB

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