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.

codec_help.go 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 codec
  18. import (
  19. "github.com/fagongzi/goetty"
  20. "github.com/fagongzi/util/hack"
  21. )
  22. // Write16String write string value with 16 byte length
  23. func Write16String(value string, buf *goetty.ByteBuf) {
  24. if value != "" {
  25. buf.WriteUInt16(uint16(len(value)))
  26. buf.WriteString(value)
  27. } else {
  28. buf.WriteUInt16(uint16(0))
  29. }
  30. }
  31. // Write16String write string value with 16 byte length
  32. func Write32String(value string, buf *goetty.ByteBuf) {
  33. if value != "" {
  34. buf.WriteUInt32(uint32(len(value)))
  35. buf.WriteString(value)
  36. } else {
  37. buf.WriteUInt32(uint32(0))
  38. }
  39. }
  40. // Write8String write string value with 8 byte length
  41. func Write8String(value string, buf *goetty.ByteBuf) {
  42. if value != "" {
  43. buf.WriteByte(uint8(len(value)))
  44. buf.WriteString(value)
  45. } else {
  46. buf.WriteByte(uint8(0))
  47. }
  48. }
  49. // ReadString read string value
  50. func ReadString(buf *goetty.ByteBuf) string {
  51. size := ReadUInt16(buf)
  52. if size == 0 {
  53. return ""
  54. }
  55. _, value, _ := buf.ReadBytes(int(size))
  56. return hack.SliceToString(value)
  57. }
  58. // MaybeReadString maybe read string value
  59. func MaybeReadString(buf *goetty.ByteBuf) (string, bool) {
  60. if buf.Readable() < 2 {
  61. return "", false
  62. }
  63. size := ReadUInt16(buf)
  64. if size == 0 {
  65. return "", true
  66. }
  67. if buf.Readable() < int(size) {
  68. return "", false
  69. }
  70. _, value, _ := buf.ReadBytes(int(size))
  71. return hack.SliceToString(value), true
  72. }
  73. // WriteBigString write big string
  74. func WriteBigString(value string, buf *goetty.ByteBuf) {
  75. if value != "" {
  76. buf.WriteInt(len(value))
  77. buf.WriteString(value)
  78. } else {
  79. buf.WriteInt(0)
  80. }
  81. }
  82. // ReadBigString read big string
  83. func ReadBigString(buf *goetty.ByteBuf) string {
  84. size := ReadInt(buf)
  85. if size == 0 {
  86. return ""
  87. }
  88. _, value, _ := buf.ReadBytes(size)
  89. return hack.SliceToString(value)
  90. }
  91. // MaybeReadBigString maybe read string value
  92. func MaybeReadBigString(buf *goetty.ByteBuf) (string, bool) {
  93. if buf.Readable() < 4 {
  94. return "", false
  95. }
  96. size := ReadInt(buf)
  97. if size == 0 {
  98. return "", true
  99. }
  100. if buf.Readable() < size {
  101. return "", false
  102. }
  103. _, value, _ := buf.ReadBytes(int(size))
  104. return hack.SliceToString(value), true
  105. }
  106. // ReadUInt64 read uint64 value
  107. func ReadUInt64(buf *goetty.ByteBuf) uint64 {
  108. value, _ := buf.ReadUInt64()
  109. return value
  110. }
  111. // ReadUInt16 read uint16 value
  112. func ReadUInt16(buf *goetty.ByteBuf) uint16 {
  113. value, _ := buf.ReadUInt16()
  114. return value
  115. }
  116. // ReadUInt32 read uint16 value
  117. func ReadUInt32(buf *goetty.ByteBuf) uint32 {
  118. value, _ := buf.ReadUInt32()
  119. return value
  120. }
  121. // ReadUInt32 read uint16 value
  122. func Read(buf *goetty.ByteBuf, p []byte) []byte {
  123. buf.Read(p)
  124. return p
  125. }
  126. // ReadInt read int value
  127. func ReadInt(buf *goetty.ByteBuf) int {
  128. value, _ := buf.ReadInt()
  129. return value
  130. }
  131. // ReadByte read byte value
  132. func ReadByte(buf *goetty.ByteBuf) byte {
  133. value, _ := buf.ReadByte()
  134. return value
  135. }
  136. // ReadBytes read bytes value
  137. func ReadBytes(n int, buf *goetty.ByteBuf) []byte {
  138. _, value, _ := buf.ReadBytes(n)
  139. return value
  140. }
  141. // WriteBool write bool value
  142. func WriteBool(value bool, out *goetty.ByteBuf) {
  143. out.WriteByte(boolToByte(value))
  144. }
  145. // WriteSlice write slice value
  146. func WriteSlice(value []byte, buf *goetty.ByteBuf) {
  147. buf.WriteUInt16(uint16(len(value)))
  148. if len(value) > 0 {
  149. buf.Write(value)
  150. }
  151. }
  152. // ReadSlice read slice value
  153. func ReadSlice(buf *goetty.ByteBuf) []byte {
  154. l, _ := buf.ReadUInt16()
  155. if l == 0 {
  156. return nil
  157. }
  158. _, data, _ := buf.ReadBytes(int(l))
  159. return data
  160. }
  161. func boolToByte(value bool) byte {
  162. if value {
  163. return 1
  164. }
  165. return 0
  166. }
  167. func byteToBool(value byte) bool {
  168. if value == 1 {
  169. return true
  170. }
  171. return false
  172. }

Go Implementation For Seata