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

buf.go 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package bytes
  18. import (
  19. "encoding/binary"
  20. "io"
  21. gxbytes "github.com/dubbogo/gost/bytes"
  22. )
  23. type ByteBuffer struct {
  24. buf *gxbytes.Buffer
  25. }
  26. func NewByteBuffer(buf []byte) *ByteBuffer {
  27. return &ByteBuffer{
  28. buf: gxbytes.NewBuffer(buf),
  29. }
  30. }
  31. func (b *ByteBuffer) Bytes() []byte {
  32. return b.buf.Bytes()
  33. }
  34. func (b *ByteBuffer) Read(p []byte) (n int, err error) {
  35. return b.buf.Read(p)
  36. }
  37. func (b *ByteBuffer) ReadByte() (byte, error) {
  38. data := make([]byte, 1)
  39. n, err := b.Read(data)
  40. if err != nil {
  41. return 0, err
  42. }
  43. if n < 1 {
  44. return 0, io.ErrShortBuffer
  45. }
  46. return data[0], nil
  47. }
  48. func (b *ByteBuffer) ReadInt64() (int64, error) {
  49. data := make([]byte, 8)
  50. n, err := b.Read(data)
  51. if err != nil {
  52. return 0, err
  53. }
  54. if n < 8 {
  55. return 0, io.ErrShortBuffer
  56. }
  57. return Byte2Int64(data), nil
  58. }
  59. func (b *ByteBuffer) ReadUint16() (uint16, error) {
  60. data := make([]byte, 2)
  61. n, err := b.Read(data)
  62. if err != nil {
  63. return 0, err
  64. }
  65. if n < 2 {
  66. return 0, io.ErrShortBuffer
  67. }
  68. return Byte2UInt16(data), nil
  69. }
  70. func (b *ByteBuffer) ReadUint32() (uint32, error) {
  71. data := make([]byte, 4)
  72. n, err := b.Read(data)
  73. if err != nil {
  74. return 0, err
  75. }
  76. if n < 2 {
  77. return 0, io.ErrShortBuffer
  78. }
  79. return Byte2UInt32(data), nil
  80. }
  81. func (b *ByteBuffer) ReadUint64() (uint64, error) {
  82. data := make([]byte, 8)
  83. n, err := b.Read(data)
  84. if err != nil {
  85. return 0, err
  86. }
  87. if n < 2 {
  88. return 0, io.ErrShortBuffer
  89. }
  90. return Byte2UInt64(data), nil
  91. }
  92. func (b *ByteBuffer) Write(p []byte) (n int, err error) {
  93. return b.buf.Write(p)
  94. }
  95. func (b *ByteBuffer) WriteString(str string) (n int, err error) {
  96. return b.buf.Write([]byte(str))
  97. }
  98. func (b *ByteBuffer) WriteByte(p byte) error {
  99. return b.buf.WriteByte(p)
  100. }
  101. func (b *ByteBuffer) WriteUint16(p uint16) (n int, err error) {
  102. return b.buf.Write(UInt16ToBytes(p))
  103. }
  104. func (b *ByteBuffer) WriteUint32(p uint32) (n int, err error) {
  105. return b.buf.Write(UInt32ToBytes(p))
  106. }
  107. func (b *ByteBuffer) WriteUint64(p uint64) (n int, err error) {
  108. return b.buf.Write(UInt64ToBytes(p))
  109. }
  110. func (b *ByteBuffer) WriteInt64(p int64) (n int, err error) {
  111. return b.buf.Write(Int64ToBytes(p))
  112. }
  113. // Byte2Int64 byte array to int64 value using big order
  114. func Byte2Int64(data []byte) int64 {
  115. return int64((int64(data[0])&0xff)<<56 |
  116. (int64(data[1])&0xff)<<48 |
  117. (int64(data[2])&0xff)<<40 |
  118. (int64(data[3])&0xff)<<32 |
  119. (int64(data[4])&0xff)<<24 |
  120. (int64(data[5])&0xff)<<16 |
  121. (int64(data[6])&0xff)<<8 |
  122. (int64(data[7]) & 0xff))
  123. }
  124. // Byte2UInt64 byte array to int64 value using big order
  125. func Byte2UInt64(data []byte) uint64 {
  126. return binary.BigEndian.Uint64(data)
  127. }
  128. // Byte2UInt16 byte array to uint16 value using big order
  129. func Byte2UInt16(data []byte) uint16 {
  130. return binary.BigEndian.Uint16(data)
  131. }
  132. // Byte2UInt32 byte array to uint32 value using big order
  133. func Byte2UInt32(data []byte) uint32 {
  134. return binary.BigEndian.Uint32(data)
  135. }
  136. // Int2BytesTo int value to bytes array using big order
  137. func Int2BytesTo(v int, ret []byte) {
  138. ret[0] = byte(v >> 24)
  139. ret[1] = byte(v >> 16)
  140. ret[2] = byte(v >> 8)
  141. ret[3] = byte(v)
  142. }
  143. // Int2Bytes int value to bytes array using big order
  144. func Int2Bytes(v int) []byte {
  145. ret := make([]byte, 4)
  146. Int2BytesTo(v, ret)
  147. return ret
  148. }
  149. // Int64ToBytesTo int64 value to bytes array using big order
  150. func Int64ToBytesTo(v int64, ret []byte) {
  151. ret[0] = byte(v >> 56)
  152. ret[1] = byte(v >> 48)
  153. ret[2] = byte(v >> 40)
  154. ret[3] = byte(v >> 32)
  155. ret[4] = byte(v >> 24)
  156. ret[5] = byte(v >> 16)
  157. ret[6] = byte(v >> 8)
  158. ret[7] = byte(v)
  159. }
  160. // Uint64ToBytesTo uint64 value to bytes array using big order
  161. func Uint64ToBytesTo(v uint64, ret []byte) {
  162. binary.BigEndian.PutUint64(ret, v)
  163. }
  164. // Int64ToBytes int64 value to bytes array using big order
  165. func Int64ToBytes(v int64) []byte {
  166. ret := make([]byte, 8)
  167. Int64ToBytesTo(v, ret)
  168. return ret
  169. }
  170. // Uint32ToBytesTo uint32 value to bytes array using big order
  171. func Uint32ToBytesTo(v uint32, ret []byte) {
  172. binary.BigEndian.PutUint32(ret, v)
  173. }
  174. // UInt32ToBytes uint32 value to bytes array using big order
  175. func UInt32ToBytes(v uint32) []byte {
  176. ret := make([]byte, 4)
  177. Uint32ToBytesTo(v, ret)
  178. return ret
  179. }
  180. // Uint16ToBytesTo uint16 value to bytes array using big order
  181. func Uint16ToBytesTo(v uint16, ret []byte) {
  182. binary.BigEndian.PutUint16(ret, v)
  183. }
  184. // UInt16ToBytes uint16 value to bytes array using big order
  185. func UInt16ToBytes(v uint16) []byte {
  186. ret := make([]byte, 2)
  187. Uint16ToBytesTo(v, ret)
  188. return ret
  189. }
  190. // UInt16ToBytes uint16 value to bytes array using big order
  191. func UInt64ToBytes(v uint64) []byte {
  192. ret := make([]byte, 8)
  193. Uint64ToBytesTo(v, ret)
  194. return ret
  195. }

Go Implementation For Seata