ByteArray.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package public
  2. import (
  3. "encoding/binary"
  4. "io"
  5. )
  6. type ByteArray struct {
  7. buf []byte
  8. posWrite int
  9. posRead int
  10. endian binary.ByteOrder
  11. }
  12. var ByteArrayEndian binary.ByteOrder = binary.LittleEndian
  13. func CreateByteArray(bytes []byte) *ByteArray {
  14. var ba *ByteArray
  15. if bytes != nil && len(bytes) > 0 {
  16. ba = &ByteArray{buf: bytes}
  17. } else {
  18. ba = &ByteArray{}
  19. }
  20. ba.endian = binary.LittleEndian
  21. return ba
  22. }
  23. func (this *ByteArray) Length() int {
  24. return len(this.buf)
  25. }
  26. func (this *ByteArray) Available() int {
  27. return this.Length() - this.posRead
  28. }
  29. func (this *ByteArray) SetEndian(endian binary.ByteOrder) {
  30. this.endian = endian
  31. }
  32. func (this *ByteArray) GetEndian() binary.ByteOrder {
  33. if this.endian == nil {
  34. return ByteArrayEndian
  35. }
  36. return this.endian
  37. }
  38. func (this *ByteArray) grow(l int) {
  39. if l == 0 {
  40. return
  41. }
  42. space := len(this.buf) - this.posWrite
  43. if space >= l {
  44. return
  45. }
  46. needGrow := l - space
  47. bufGrow := make([]byte, needGrow)
  48. this.buf = append(this.buf, bufGrow...)
  49. }
  50. func (this *ByteArray) SetWritePos(pos int) error {
  51. if pos > this.Length() {
  52. this.posWrite = this.Length()
  53. return io.EOF
  54. } else {
  55. this.posWrite = pos
  56. }
  57. return nil
  58. }
  59. func (this *ByteArray) SetWriteEnd() {
  60. this.SetWritePos(this.Length())
  61. }
  62. func (this *ByteArray) GetWritePos() int {
  63. return this.posWrite
  64. }
  65. func (this *ByteArray) SetReadPos(pos int) error {
  66. if pos > this.Length() {
  67. this.posRead = this.Length()
  68. return io.EOF
  69. } else {
  70. this.posRead = pos
  71. }
  72. return nil
  73. }
  74. func (this *ByteArray) SetReadEnd() {
  75. this.SetReadPos(this.Length())
  76. }
  77. func (this *ByteArray) GetReadPos() int {
  78. return this.posRead
  79. }
  80. func (this *ByteArray) Seek(pos int) error {
  81. err := this.SetWritePos(pos)
  82. this.SetReadPos(pos)
  83. return err
  84. }
  85. func (this *ByteArray) Reset() {
  86. this.buf = []byte{}
  87. this.Seek(0)
  88. }
  89. func (this *ByteArray) Bytes() []byte {
  90. return this.buf
  91. }
  92. func (this *ByteArray) BytesAvailable() []byte {
  93. return this.buf[this.posRead:]
  94. }
  95. //==========write
  96. func (this *ByteArray) Write(bytes []byte) (l int, err error) {
  97. this.grow(len(bytes))
  98. l = copy(this.buf[this.posWrite:], bytes)
  99. this.posWrite += l
  100. return l, nil
  101. }
  102. func (this *ByteArray) WriteBytes(bytes []byte) (l int, err error) {
  103. return this.Write(bytes)
  104. }
  105. func (this *ByteArray) WriteByte(b byte) error {
  106. bytes := make([]byte, 1)
  107. bytes[0] = b
  108. _, err := this.WriteBytes(bytes)
  109. return err
  110. }
  111. func (this *ByteArray) WriteInt8(value int8) {
  112. binary.Write(this, this.endian, &value)
  113. }
  114. func (this *ByteArray) WriteUInt8(value uint8) {
  115. binary.Write(this, this.endian, &value)
  116. }
  117. func (this *ByteArray) WriteInt16(value int16) {
  118. binary.Write(this, this.endian, &value)
  119. }
  120. func (this *ByteArray) WriteUInt16(value uint16) {
  121. binary.Write(this, this.endian, &value)
  122. }
  123. func (this *ByteArray) WriteInt32(value int32) {
  124. binary.Write(this, this.endian, &value)
  125. }
  126. func (this *ByteArray) WriteUInt32(value uint32) {
  127. binary.Write(this, this.endian, &value)
  128. }
  129. /*func (this *ByteArray) WriteInt(value int) {
  130. binary.Write(this, this.endian, &value)
  131. }
  132. func (this *ByteArray) WriteUInt(value uint) {
  133. binary.Write(this, this.endian, &value)
  134. }
  135. */
  136. func (this *ByteArray) WriteInt64(value int64) {
  137. binary.Write(this, this.endian, &value)
  138. }
  139. func (this *ByteArray) WriteFloat32(value float32) {
  140. binary.Write(this, this.endian, &value)
  141. }
  142. func (this *ByteArray) WriteFloat64(value float64) {
  143. binary.Write(this, this.endian, &value)
  144. }
  145. func (this *ByteArray) WriteBool(value bool) {
  146. var bb byte
  147. if value {
  148. bb = 1
  149. } else {
  150. bb = 0
  151. }
  152. this.WriteByte(bb)
  153. }
  154. func (this *ByteArray) WriteString(value string) {
  155. this.WriteBytes([]byte(value))
  156. }
  157. func (this *ByteArray) WriteStringWithLen(value string, stringLen int) {
  158. byString := []byte(value)
  159. this.WriteBytes(byString)
  160. for i := 0; i < stringLen-len(byString); i++ {
  161. this.WriteUInt8(0)
  162. }
  163. }
  164. func (this *ByteArray) WriteUTF(value string) {
  165. this.WriteInt16(int16(len(value)))
  166. this.WriteBytes([]byte(value))
  167. }
  168. //==========read
  169. func (this *ByteArray) Read(bytes []byte) (l int, err error) {
  170. if len(bytes) == 0 {
  171. return
  172. }
  173. if len(bytes) > this.Length()-this.posRead {
  174. return 0, io.EOF
  175. }
  176. l = copy(bytes, this.buf[this.posRead:])
  177. this.posRead += l
  178. return l, nil
  179. }
  180. func (this *ByteArray) ReadBytes(bytes []byte, length int, offset int) (l int, err error) {
  181. return this.Read(bytes[offset : offset+length])
  182. }
  183. func (this *ByteArray) ReadByte() (b byte, err error) {
  184. bytes := make([]byte, 1)
  185. _, err = this.ReadBytes(bytes, 1, 0)
  186. if err == nil {
  187. b = bytes[0]
  188. }
  189. return
  190. }
  191. func (this *ByteArray) ReadInt8() (ret int8, err error) {
  192. err = binary.Read(this, this.endian, &ret)
  193. return
  194. }
  195. func (this *ByteArray) ReadUInt8() (ret uint8, err error) {
  196. err = binary.Read(this, this.endian, &ret)
  197. return
  198. }
  199. func (this *ByteArray) ReadInt16() (ret int16, err error) {
  200. err = binary.Read(this, this.endian, &ret)
  201. return
  202. }
  203. func (this *ByteArray) ReadUInt16() (ret uint16, err error) {
  204. err = binary.Read(this, this.endian, &ret)
  205. return
  206. }
  207. func (this *ByteArray) ReadInt32() (ret int32, err error) {
  208. err = binary.Read(this, this.endian, &ret)
  209. return
  210. }
  211. func (this *ByteArray) ReadUInt32() (ret uint32, err error) {
  212. err = binary.Read(this, this.endian, &ret)
  213. return
  214. }
  215. func (this *ByteArray) ReadInt64() (ret int64, err error) {
  216. err = binary.Read(this, this.endian, &ret)
  217. return
  218. }
  219. func (this *ByteArray) ReadFloat32() (ret float32, err error) {
  220. err = binary.Read(this, this.endian, &ret)
  221. return
  222. }
  223. func (this *ByteArray) ReadFloat64() (ret float64, err error) {
  224. err = binary.Read(this, this.endian, &ret)
  225. return
  226. }
  227. func (this *ByteArray) ReadBool() (ret bool, err error) {
  228. var bb byte
  229. bb, err = this.ReadByte()
  230. if err == nil {
  231. if bb == 1 {
  232. ret = true
  233. } else {
  234. ret = false
  235. }
  236. } else {
  237. ret = false
  238. }
  239. return
  240. }
  241. func (this *ByteArray) ReadString(length int) (ret string, err error) {
  242. bytes := make([]byte, length)
  243. _, err = this.ReadBytes(bytes, length, 0)
  244. if err == nil {
  245. // 有可能有末尾的多余空格
  246. for i := 0; i < length; i++ {
  247. if bytes[i] == 0 {
  248. bytes = bytes[:i]
  249. break
  250. }
  251. }
  252. ret = string(bytes)
  253. } else {
  254. ret = ""
  255. }
  256. return
  257. }
  258. func (this *ByteArray) ReadUTF() (ret string, err error) {
  259. var l int16
  260. l, err = this.ReadInt16()
  261. if err != nil {
  262. return "", err
  263. }
  264. return this.ReadString(int(l))
  265. }