buffer.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package util
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "io"
  6. "math"
  7. "net"
  8. )
  9. // Buffer 用于方便自动扩容的内存写入,已经读取
  10. type Buffer []byte
  11. // ReuseBuffer 重用buffer,内容可能会被覆盖,要尽早复制
  12. type ReuseBuffer struct {
  13. Buffer
  14. }
  15. func (ReuseBuffer) Reuse() bool {
  16. return true
  17. }
  18. // LimitBuffer 限制buffer的长度,不会改变原来的buffer,防止内存泄漏
  19. type LimitBuffer struct {
  20. Buffer
  21. }
  22. func (b *LimitBuffer) ReadN(n int) (result LimitBuffer) {
  23. result.Buffer = b.Buffer.ReadN(n)
  24. return
  25. }
  26. func (b LimitBuffer) Clone() (result LimitBuffer) {
  27. result.Buffer = b.Buffer.Clone()
  28. return
  29. }
  30. func (b LimitBuffer) SubBuf(start int, length int) (result LimitBuffer) {
  31. result.Buffer = b.Buffer.SubBuf(start, length)
  32. return
  33. }
  34. func (b *LimitBuffer) Malloc(count int) (result LimitBuffer) {
  35. l := b.Len()
  36. newL := l + count
  37. if c := b.Cap(); newL > c {
  38. panic(fmt.Sprintf("LimitBuffer Malloc %d > %d", newL, c))
  39. } else {
  40. *b = b.SubBuf(0, newL)
  41. }
  42. return b.SubBuf(l, count)
  43. }
  44. func (b *LimitBuffer) Write(a []byte) (n int, err error) {
  45. l := b.Len()
  46. newL := l + len(a)
  47. if c := b.Cap(); newL > c {
  48. return 0, fmt.Errorf("LimitBuffer Write %d > %d", newL, c)
  49. // panic(fmt.Sprintf("LimitBuffer Write %d > %d", newL, c))
  50. } else {
  51. b.Buffer = b.Buffer.SubBuf(0, newL)
  52. copy(b.Buffer[l:], a)
  53. }
  54. return len(a), nil
  55. }
  56. // IBytes 用于区分传入的内存是否是复用内存,例如从网络中读取的数据,如果是复用内存,需要尽早复制
  57. type IBytes interface {
  58. Len() int
  59. Bytes() []byte
  60. Reuse() bool
  61. }
  62. type IBuffer interface {
  63. Len() int
  64. Bytes() []byte
  65. Reuse() bool
  66. SubBuf(start int, length int) Buffer
  67. Malloc(count int) Buffer
  68. Reset()
  69. WriteUint32(v uint32)
  70. WriteUint24(v uint32)
  71. WriteUint16(v uint16)
  72. WriteFloat64(v float64)
  73. WriteByte(v byte)
  74. WriteString(a string)
  75. Write(a []byte) (n int, err error)
  76. ReadN(n int) Buffer
  77. ReadFloat64() float64
  78. ReadUint64() uint64
  79. ReadUint32() uint32
  80. ReadUint24() uint32
  81. ReadUint16() uint16
  82. ReadByte() byte
  83. Read(buf []byte) (n int, err error)
  84. Clone() Buffer
  85. CanRead() bool
  86. CanReadN(n int) bool
  87. Cap() int
  88. }
  89. func (Buffer) Reuse() bool {
  90. return false
  91. }
  92. func (b *Buffer) Read(buf []byte) (n int, err error) {
  93. if !b.CanReadN(len(buf)) {
  94. copy(buf, *b)
  95. return b.Len(), io.EOF
  96. }
  97. ret := b.ReadN(len(buf))
  98. copy(buf, ret)
  99. return len(ret), err
  100. }
  101. func (b *Buffer) ReadN(n int) Buffer {
  102. l := b.Len()
  103. r := (*b)[:n]
  104. *b = (*b)[n:l]
  105. return r
  106. }
  107. func (b *Buffer) ReadFloat64() float64 {
  108. return math.Float64frombits(b.ReadUint64())
  109. }
  110. func (b *Buffer) ReadUint64() uint64 {
  111. return binary.BigEndian.Uint64(b.ReadN(8))
  112. }
  113. func (b *Buffer) ReadUint32() uint32 {
  114. return binary.BigEndian.Uint32(b.ReadN(4))
  115. }
  116. func (b *Buffer) ReadUint24() uint32 {
  117. return ReadBE[uint32](b.ReadN(3))
  118. }
  119. func (b *Buffer) ReadUint16() uint16 {
  120. return binary.BigEndian.Uint16(b.ReadN(2))
  121. }
  122. func (b *Buffer) ReadByte() byte {
  123. return b.ReadN(1)[0]
  124. }
  125. func (b *Buffer) WriteFloat64(v float64) {
  126. PutBE(b.Malloc(8), math.Float64bits(v))
  127. }
  128. func (b *Buffer) WriteUint32(v uint32) {
  129. binary.BigEndian.PutUint32(b.Malloc(4), v)
  130. }
  131. func (b *Buffer) WriteUint24(v uint32) {
  132. PutBE(b.Malloc(3), v)
  133. }
  134. func (b *Buffer) WriteUint16(v uint16) {
  135. binary.BigEndian.PutUint16(b.Malloc(2), v)
  136. }
  137. func (b *Buffer) WriteByte(v byte) {
  138. b.Malloc(1)[0] = v
  139. }
  140. func (b *Buffer) WriteString(a string) {
  141. *b = append(*b, a...)
  142. }
  143. func (b *Buffer) Write(a []byte) (n int, err error) {
  144. l := b.Len()
  145. newL := l + len(a)
  146. if newL > b.Cap() {
  147. *b = append(*b, a...)
  148. } else {
  149. *b = b.SubBuf(0, newL)
  150. copy((*b)[l:], a)
  151. }
  152. return len(a), nil
  153. }
  154. func (b Buffer) Clone() (result Buffer) {
  155. return append(result, b...)
  156. }
  157. func (b Buffer) Bytes() []byte {
  158. return b
  159. }
  160. func (b Buffer) Len() int {
  161. return len(b)
  162. }
  163. func (b Buffer) CanRead() bool {
  164. return b.CanReadN(1)
  165. }
  166. func (b Buffer) CanReadN(n int) bool {
  167. return b.Len() >= n
  168. }
  169. func (b Buffer) Cap() int {
  170. return cap(b)
  171. }
  172. func (b Buffer) SubBuf(start int, length int) Buffer {
  173. return b[start : start+length]
  174. }
  175. // Malloc 扩大原来的buffer的长度,返回新增的buffer
  176. func (b *Buffer) Malloc(count int) Buffer {
  177. l := b.Len()
  178. newL := l + count
  179. if newL > b.Cap() {
  180. n := make(Buffer, newL)
  181. copy(n, *b)
  182. *b = n
  183. } else {
  184. *b = b.SubBuf(0, newL)
  185. }
  186. return b.SubBuf(l, count)
  187. }
  188. // Relloc 改变 buffer 到指定大小
  189. func (b *Buffer) Relloc(count int) {
  190. b.Reset()
  191. b.Malloc(count)
  192. }
  193. func (b *Buffer) Reset() {
  194. *b = b.SubBuf(0, 0)
  195. }
  196. func (b *Buffer) Split(n int) (result net.Buffers) {
  197. origin := *b
  198. for {
  199. if b.CanReadN(n) {
  200. result = append(result, b.ReadN(n))
  201. } else {
  202. result = append(result, *b)
  203. *b = origin
  204. return
  205. }
  206. }
  207. }
  208. // ConcatBuffers 合并碎片内存为一个完整内存
  209. func ConcatBuffers[T ~[]byte](input []T) (out []byte) {
  210. for _, v := range input {
  211. out = append(out, v...)
  212. }
  213. return
  214. }
  215. // SizeOfBuffers 计算Buffers的内容长度
  216. func SizeOfBuffers[T ~[]byte](buf []T) (size int) {
  217. for _, b := range buf {
  218. size += len(b)
  219. }
  220. return
  221. }
  222. // SplitBuffers 按照一定大小分割 Buffers
  223. func SplitBuffers[T ~[]byte](buf []T, size int) (result [][]T) {
  224. buf = append([]T(nil), buf...)
  225. for total := SizeOfBuffers(buf); total > 0; {
  226. if total <= size {
  227. return append(result, buf)
  228. } else {
  229. var before []T
  230. sizeOfBefore := 0
  231. for _, b := range buf {
  232. need := size - sizeOfBefore
  233. if lenOfB := len(b); lenOfB > need {
  234. before = append(before, b[:need])
  235. result = append(result, before)
  236. total -= need
  237. buf[0] = b[need:]
  238. break
  239. } else {
  240. sizeOfBefore += lenOfB
  241. before = append(before, b)
  242. total -= lenOfB
  243. buf = buf[1:]
  244. }
  245. }
  246. }
  247. }
  248. return
  249. }