用golang写了个仿AS3写的ByteArray,稍微有点差别,demo能成功运行,还未进行其他测试

主要参考的是golang自带库里的Buffer,结合了binary

来看看demo:

  1. package main
  2.  
  3. import (
  4. "tbs"
  5. "fmt"
  6. )
  7.  
  8. func main() {
  9. var ba *tbs.ByteArray = tbs.CreateByteArray([]byte{})
  10.  
  11. ba.WriteBytes([]byte("abc"))
  12. ba.WriteByte('A')
  13. ba.WriteBool(true)
  14. ba.WriteBool(false)
  15. ba.WriteInt8()
  16. ba.WriteInt16()
  17. ba.WriteInt32()
  18. ba.WriteInt64()
  19. ba.WriteFloat32(123.456)
  20. ba.WriteFloat64(456.789)
  21. ba.WriteString("hello ")
  22. ba.WriteUTF("world!")
  23.  
  24. bytes := make([]byte, )
  25. fmt.Println(ba.ReadBytes(bytes, , ))
  26. fmt.Println(ba.ReadByte())
  27. fmt.Println(ba.ReadBool())
  28. fmt.Println(ba.ReadBool())
  29. fmt.Println(ba.ReadInt8())
  30. fmt.Println(ba.ReadInt16())
  31. fmt.Println(ba.ReadInt32())
  32. fmt.Println(ba.ReadInt64())
  33. fmt.Println(ba.ReadFloat32())
  34. fmt.Println(ba.ReadFloat64())
  35. fmt.Println(ba.ReadString())
  36. fmt.Println(ba.ReadUTF())
  37.  
  38. byte,err := ba.ReadByte()
  39. if err == nil{
  40. fmt.Println(byte)
  41. }else{
  42. fmt.Println("end of file")
  43. }
  44.  
  45. ba.Seek() //back to 3
  46. fmt.Println(ba.ReadByte())
  47. ba.Seek() //back to 39
  48. fmt.Printf("ba has %d bytes available!\n", ba.Available())
  49. fmt.Println(ba.ReadUTF())
  50. }

demo中测试所有的方法

付上代码:

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

我用两个游标来控制,你可能会不习惯,但用起来会更加灵活!

我现在已经把ByteArray放在我的项目中了,已经完全整合进去了。

(如果要用这个类,那你可以做任意修改)
贴上我自己的博客地址:http://blog.codeforever.net/

golang仿AS3写的ByteArray的更多相关文章

  1. WIFI_仿手机写wifi应用程序_WDS

    2-1.1_15节_使用WIFI网卡6_仿手机写wifi操作程序============================== 1. 仿手机写一个WIFI操作程序,作为STA,有这几个功能:a. 自动扫 ...

  2. 仿flask写的web框架

    某大佬仿flask写的web框架 web_frame.py from werkzeug.local import LocalStack, LocalProxy def get_request_cont ...

  3. As3截图转换为ByteArray传送给后台node的一种方法

    最近将以前用As3+Php做的一个画板拿出来改成了As3+nodejs(expressjs4). Node: 1. 将图片存放的路径设置为静态公开的路径. app.use(express.static ...

  4. windows从0开始学golang--0--安装golang+git+自己写包

    windows下 1.安装golang 2.安装git(主要是go get 引用git上的包) 3.  使用默认安装生成的目录 pkg:包含包对象,编译好的库文件 src:包含 Go 源文件,注意:你 ...

  5. golang 仿python pack/unpack

    写得不完善也不完美 尤其是高低位转换那(go和c 二进制高地位相反 需要转换,还有go int转[]byte长度是4位),希望牛人看后指导一下 项目需要通过socket调取 客户端是go ,服务器端是 ...

  6. Golang学习---test写法和benchmark写法

    一.Test 1. 每一个test文件须import一个testing 2. test文件下的每一个test case 均必须用Test开头并且符合TestXxxx形式,否则go test会直接跳过测 ...

  7. 仿log4j 写 log 代码

    log4j 一直有个问题无法满足我,有可能我还不知道怎么去使用它. 就是他会把项目中所有的日志信息都记到一个文件里面,而业务系统往往需要根据某个业务流程查看日志分析. public class Bus ...

  8. Golang仿云盘项目-2.2 保留文件元信息

    本文来自博客园,作者:Jayvee,转载请注明原文链接:https://www.cnblogs.com/cenjw/p/16459817.html 目录结构 E:\goproj\FileStorage ...

  9. [转]关于AS3 Socket和TCP不得不说的三两事

    磨刀不误砍柴工,让我们从概念入手,逐步深入. 所谓socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络 ...

随机推荐

  1. jquery实现div垂直居中

    <html> <head> <meta charset="UTF-8"> <title></title> <scr ...

  2. Android Studio 打包流程

    (1)Android Studio菜单Build->Generate Signed APK (2)弹出窗口 (3)创建密钥库及密钥,创建后会自动选择刚创建的密钥库和密钥(已拥有密钥库跳过)    ...

  3. 在win8.1下安装laravel5.1时碰到的坑不少,但总算搞掂,真有点不容易。

    安装好php后,安装laravel的方法有如下几种. 1.先安装好composer, 再用composer下载资源并安装,命令如下: composer create-project laravel/l ...

  4. phpcms在自定义模块中的自定义标签分页

    如果你是一个经验丰富的phpcms二次开发人员,本篇文章可以忽略不计,因为这里的写法自己都觉得很恶心        今天在开发一个网站自建了一个模块叫做论坛模块,目录名称:luntan        ...

  5. undo_retention:确定最优的撤销保留时间

    使用下面的公式来计算undo_retention参数的值: undo_retention=undo size/(db_block_size * undo_block_per_sec) 可以通过提交下面 ...

  6. EL表达式(转)

    转自:http://www.cnblogs.com/Fskjb/archive/2009/07/05/1517192.html EL 全名为Expression Language EL 语法很简单,它 ...

  7. Oracle EBS-SQL (BOM-8):检查物料属性(无采购员).sql

    select       msi.segment1                                  物料编码,       msi.DESCRIPTION               ...

  8. SQL PLUS远程连接

    http://blog.csdn.net/wildin/article/details/5850252 这篇文章无敌了. Oracle sqlplus添加历史记录功能: http://www.cnbl ...

  9. javascript预加载和延迟加载

    延迟加载javascript,也就是页面加载完成之后再加载javascript,也叫on demand(按需)加载,一般有一下几个方法: What can your tired old page, o ...

  10. 用ATL和MFC来创建ActiveX控件

    摘要:目前MFC和ATL代表了两种框架,分别面向不同类型的基于Windows的开发.MFC代表了创建独立的Windows应用的一种简单.一致的方法:ATL提供了一种框架来实现创建COM客户机和服务器所 ...