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.

64 lines
1.7 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
  1. package binary
  2. import (
  3. "io"
  4. )
  5. const (
  6. TYPE_NIL = Byte(0x00)
  7. TYPE_BYTE = Byte(0x01)
  8. TYPE_INT8 = Byte(0x02)
  9. TYPE_UINT8 = Byte(0x03)
  10. TYPE_INT16 = Byte(0x04)
  11. TYPE_UINT16 = Byte(0x05)
  12. TYPE_INT32 = Byte(0x06)
  13. TYPE_UINT32 = Byte(0x07)
  14. TYPE_INT64 = Byte(0x08)
  15. TYPE_UINT64 = Byte(0x09)
  16. TYPE_STRING = Byte(0x10)
  17. TYPE_BYTESLICE = Byte(0x11)
  18. )
  19. func GetBinaryType(o Binary) Byte {
  20. switch o.(type) {
  21. case nil: return TYPE_NIL
  22. case Byte: return TYPE_BYTE
  23. case Int8: return TYPE_INT8
  24. case UInt8: return TYPE_UINT8
  25. case Int16: return TYPE_INT16
  26. case UInt16: return TYPE_UINT16
  27. case Int32: return TYPE_INT32
  28. case UInt32: return TYPE_UINT32
  29. case Int64: return TYPE_INT64
  30. case UInt64: return TYPE_UINT64
  31. case Int: panic("Int not supported")
  32. case UInt: panic("UInt not supported")
  33. case String: return TYPE_STRING
  34. case ByteSlice: return TYPE_BYTESLICE
  35. default: panic("Unsupported type")
  36. }
  37. }
  38. func ReadBinary(r io.Reader) Binary {
  39. type_ := ReadByte(r)
  40. switch type_ {
  41. case TYPE_NIL: return nil
  42. case TYPE_BYTE: return ReadByte(r)
  43. case TYPE_INT8: return ReadInt8(r)
  44. case TYPE_UINT8: return ReadUInt8(r)
  45. case TYPE_INT16: return ReadInt16(r)
  46. case TYPE_UINT16: return ReadUInt16(r)
  47. case TYPE_INT32: return ReadInt32(r)
  48. case TYPE_UINT32: return ReadUInt32(r)
  49. case TYPE_INT64: return ReadInt64(r)
  50. case TYPE_UINT64: return ReadUInt64(r)
  51. case TYPE_STRING: return ReadString(r)
  52. case TYPE_BYTESLICE:return ReadByteSlice(r)
  53. default: panic("Unsupported type")
  54. }
  55. }