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.

122 lines
2.6 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
11 years ago
11 years ago
11 years ago
  1. package binary
  2. import (
  3. "encoding/binary"
  4. "io"
  5. )
  6. // Byte
  7. func WriteByte(w io.Writer, b byte, n *int64, err *error) {
  8. WriteTo(w, []byte{b}, n, err)
  9. }
  10. func ReadByte(r io.Reader, n *int64, err *error) byte {
  11. buf := make([]byte, 1)
  12. ReadFull(r, buf, n, err)
  13. return buf[0]
  14. }
  15. // Int8
  16. func WriteInt8(w io.Writer, i int8, n *int64, err *error) {
  17. WriteByte(w, byte(i), n, err)
  18. }
  19. func ReadInt8(r io.Reader, n *int64, err *error) int8 {
  20. return int8(ReadByte(r, n, err))
  21. }
  22. // UInt8
  23. func WriteUInt8(w io.Writer, i uint8, n *int64, err *error) {
  24. WriteByte(w, byte(i), n, err)
  25. }
  26. func ReadUInt8(r io.Reader, n *int64, err *error) uint8 {
  27. return uint8(ReadByte(r, n, err))
  28. }
  29. // Int16
  30. func WriteInt16(w io.Writer, i int16, n *int64, err *error) {
  31. buf := make([]byte, 2)
  32. binary.LittleEndian.PutUint16(buf, uint16(i))
  33. WriteTo(w, buf, n, err)
  34. }
  35. func ReadInt16(r io.Reader, n *int64, err *error) int16 {
  36. buf := make([]byte, 2)
  37. ReadFull(r, buf, n, err)
  38. return int16(binary.LittleEndian.Uint16(buf))
  39. }
  40. // UInt16
  41. func WriteUInt16(w io.Writer, i uint16, n *int64, err *error) {
  42. buf := make([]byte, 2)
  43. binary.LittleEndian.PutUint16(buf, uint16(i))
  44. WriteTo(w, buf, n, err)
  45. }
  46. func ReadUInt16(r io.Reader, n *int64, err *error) uint16 {
  47. buf := make([]byte, 2)
  48. ReadFull(r, buf, n, err)
  49. return uint16(binary.LittleEndian.Uint16(buf))
  50. }
  51. // Int32
  52. func WriteInt32(w io.Writer, i int32, n *int64, err *error) {
  53. buf := make([]byte, 4)
  54. binary.LittleEndian.PutUint32(buf, uint32(i))
  55. WriteTo(w, buf, n, err)
  56. }
  57. func ReadInt32(r io.Reader, n *int64, err *error) int32 {
  58. buf := make([]byte, 4)
  59. ReadFull(r, buf, n, err)
  60. return int32(binary.LittleEndian.Uint32(buf))
  61. }
  62. // UInt32
  63. func WriteUInt32(w io.Writer, i uint32, n *int64, err *error) {
  64. buf := make([]byte, 4)
  65. binary.LittleEndian.PutUint32(buf, uint32(i))
  66. WriteTo(w, buf, n, err)
  67. }
  68. func ReadUInt32(r io.Reader, n *int64, err *error) uint32 {
  69. buf := make([]byte, 4)
  70. ReadFull(r, buf, n, err)
  71. return uint32(binary.LittleEndian.Uint32(buf))
  72. }
  73. // Int64
  74. func WriteInt64(w io.Writer, i int64, n *int64, err *error) {
  75. buf := make([]byte, 8)
  76. binary.LittleEndian.PutUint64(buf, uint64(i))
  77. WriteTo(w, buf, n, err)
  78. }
  79. func ReadInt64(r io.Reader, n *int64, err *error) int64 {
  80. buf := make([]byte, 8)
  81. ReadFull(r, buf, n, err)
  82. return int64(binary.LittleEndian.Uint64(buf))
  83. }
  84. // UInt64
  85. func WriteUInt64(w io.Writer, i uint64, n *int64, err *error) {
  86. buf := make([]byte, 8)
  87. binary.LittleEndian.PutUint64(buf, uint64(i))
  88. WriteTo(w, buf, n, err)
  89. }
  90. func ReadUInt64(r io.Reader, n *int64, err *error) uint64 {
  91. buf := make([]byte, 8)
  92. ReadFull(r, buf, n, err)
  93. return uint64(binary.LittleEndian.Uint64(buf))
  94. }