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_helper.go 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. func ReadBytes(n int, buf *ByteBuffer) []byte {
  19. bytes := make([]byte, n)
  20. buf.Read(bytes)
  21. return bytes
  22. }
  23. func ReadByte(buf *ByteBuffer) byte {
  24. value, _ := buf.ReadByte()
  25. return value
  26. }
  27. func ReadUint8(buf *ByteBuffer) uint8 {
  28. value, _ := buf.ReadByte()
  29. return value
  30. }
  31. func ReadUInt16(buf *ByteBuffer) uint16 {
  32. value, _ := buf.ReadUint16()
  33. return value
  34. }
  35. func ReadUInt32(buf *ByteBuffer) uint32 {
  36. value, _ := buf.ReadUint32()
  37. return value
  38. }
  39. func ReadUInt64(buf *ByteBuffer) uint64 {
  40. value, _ := buf.ReadUint64()
  41. return value
  42. }
  43. func ReadString8(buf *ByteBuffer) string {
  44. bytes := make([]byte, 1)
  45. buf.Read(bytes)
  46. return string(bytes)
  47. }
  48. func Read1String16(buf *ByteBuffer) string {
  49. bytes := make([]byte, 2)
  50. buf.Read(bytes)
  51. return string(bytes)
  52. }
  53. func ReadString32(buf *ByteBuffer) string {
  54. bytes := make([]byte, 4)
  55. buf.Read(bytes)
  56. return string(bytes)
  57. }
  58. func ReadString64(buf *ByteBuffer) string {
  59. bytes := make([]byte, 8)
  60. buf.Read(bytes)
  61. return string(bytes)
  62. }
  63. func ReadString8Length(buf *ByteBuffer) string {
  64. length, _ := buf.ReadByte()
  65. if length > 0 {
  66. p := make([]byte, length)
  67. buf.Read(p)
  68. return string(p)
  69. }
  70. return ""
  71. }
  72. func ReadString16Length(buf *ByteBuffer) string {
  73. length, _ := buf.ReadUint16()
  74. if length > 0 {
  75. p := make([]byte, length)
  76. buf.Read(p)
  77. return string(p)
  78. }
  79. return ""
  80. }
  81. func ReadString32Length(buf *ByteBuffer) string {
  82. length, _ := buf.ReadUint32()
  83. if length > 0 {
  84. p := make([]byte, length)
  85. buf.Read(p)
  86. return string(p)
  87. }
  88. return ""
  89. }
  90. func ReadString64Length(buf *ByteBuffer) string {
  91. length, _ := buf.ReadUint64()
  92. if length > 0 {
  93. p := make([]byte, length)
  94. buf.Read(p)
  95. return string(p)
  96. }
  97. return ""
  98. }
  99. func WriteString8Length(value string, buf *ByteBuffer) {
  100. if value != "" {
  101. buf.WriteByte(byte(len(value)))
  102. buf.WriteString(value)
  103. } else {
  104. buf.WriteByte(byte(0))
  105. }
  106. }
  107. func WriteString16Length(value string, buf *ByteBuffer) {
  108. if value != "" {
  109. buf.WriteUint16(uint16(len(value)))
  110. buf.WriteString(value)
  111. } else {
  112. buf.WriteUint16(uint16(0))
  113. }
  114. }
  115. func WriteString32Length(value string, buf *ByteBuffer) {
  116. if value != "" {
  117. buf.WriteUint32(uint32(len(value)))
  118. buf.WriteString(value)
  119. } else {
  120. buf.WriteUint32(uint32(0))
  121. }
  122. }
  123. func WriteString64Length(value string, buf *ByteBuffer) {
  124. if value != "" {
  125. buf.WriteUint64(uint64(len(value)))
  126. buf.WriteString(value)
  127. } else {
  128. buf.WriteUint64(uint64(0))
  129. }
  130. }