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

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

来看看demo:

 package main

 import (
"tbs"
"fmt"
) func main() {
var ba *tbs.ByteArray = tbs.CreateByteArray([]byte{}) ba.WriteBytes([]byte("abc"))
ba.WriteByte('A')
ba.WriteBool(true)
ba.WriteBool(false)
ba.WriteInt8()
ba.WriteInt16()
ba.WriteInt32()
ba.WriteInt64()
ba.WriteFloat32(123.456)
ba.WriteFloat64(456.789)
ba.WriteString("hello ")
ba.WriteUTF("world!") bytes := make([]byte, )
fmt.Println(ba.ReadBytes(bytes, , ))
fmt.Println(ba.ReadByte())
fmt.Println(ba.ReadBool())
fmt.Println(ba.ReadBool())
fmt.Println(ba.ReadInt8())
fmt.Println(ba.ReadInt16())
fmt.Println(ba.ReadInt32())
fmt.Println(ba.ReadInt64())
fmt.Println(ba.ReadFloat32())
fmt.Println(ba.ReadFloat64())
fmt.Println(ba.ReadString())
fmt.Println(ba.ReadUTF()) byte,err := ba.ReadByte()
if err == nil{
fmt.Println(byte)
}else{
fmt.Println("end of file")
} ba.Seek() //back to 3
fmt.Println(ba.ReadByte())
ba.Seek() //back to 39
fmt.Printf("ba has %d bytes available!\n", ba.Available())
fmt.Println(ba.ReadUTF())
}

demo中测试所有的方法

付上代码:

 package tbs

 import (
"encoding/binary"
"io"
) type ByteArray struct {
buf []byte
posWrite int
posRead int
endian binary.ByteOrder
} var ByteArrayEndian binary.ByteOrder = binary.BigEndian func CreateByteArray(bytes []byte) *ByteArray {
var ba *ByteArray
if len(bytes) > {
ba = &ByteArray{buf: bytes}
} else {
ba = &ByteArray{}
} ba.endian = binary.BigEndian return ba
} func (this *ByteArray) Length() int {
return len(this.buf)
} func (this *ByteArray) Available() int {
return this.Length() - this.posRead
} func (this *ByteArray) SetEndian(endian binary.ByteOrder) {
this.endian = endian
} func (this *ByteArray) GetEndian() binary.ByteOrder {
if this.endian == nil {
return ByteArrayEndian
}
return this.endian
} func (this *ByteArray) grow(l int) {
if l == {
return
}
space := len(this.buf) - this.posWrite
if space >= l {
return
} needGrow := l - space
bufGrow := make([]byte, needGrow) this.buf = Merge(this.buf, bufGrow)
} func (this *ByteArray) SetWritePos(pos int) error{
if pos > this.Length(){
this.posWrite = this.Length()
return io.EOF
}else{
this.posWrite = pos
}
return nil
} func (this *ByteArray) SetWriteEnd(){
this.SetWritePos(this.Length())
} func (this *ByteArray) GetWritePos() int{
return this.posWrite
} func (this *ByteArray) SetReadPos(pos int) error{
if pos > this.Length(){
this.posRead = this.Length()
return io.EOF
}else{
this.posRead = pos
}
return nil
} func (this *ByteArray) SetReadEnd(){
this.SetReadPos(this.Length())
} func (this *ByteArray) GetReadPos() int{
return this.posRead
} func (this *ByteArray) Seek(pos int) error{
err := this.SetWritePos(pos)
this.SetReadPos(pos) return err
} func (this *ByteArray) Reset() {
this.buf = []byte{}
this.Seek()
} func (this *ByteArray) Bytes() []byte {
return this.buf
} func (this *ByteArray) BytesAvailable() []byte {
return this.buf[this.posRead:]
}
//==========write
func (this *ByteArray) Write(bytes []byte) (l int, err error) {
this.grow(len(bytes)) l = copy(this.buf[this.posWrite:], bytes)
this.posWrite += l return l, nil
} func (this *ByteArray) WriteBytes(bytes []byte) (l int, err error) {
return this.Write(bytes)
} func (this *ByteArray) WriteByte(b byte) {
bytes := make([]byte, )
bytes[] = b
this.WriteBytes(bytes)
} func (this *ByteArray) WriteInt8(value int8) {
binary.Write(this, this.endian, &value)
} func (this *ByteArray) WriteInt16(value int16){
binary.Write(this, this.endian, &value)
} func (this *ByteArray) WriteInt32(value int32){
binary.Write(this, this.endian, &value)
} func (this *ByteArray) WriteInt64(value int64){
binary.Write(this, this.endian, &value)
} func (this *ByteArray) WriteFloat32(value float32){
binary.Write(this, this.endian, &value)
} func (this *ByteArray) WriteFloat64(value float64){
binary.Write(this, this.endian, &value)
} func (this *ByteArray) WriteBool(value bool){
var bb byte
if value {
bb =
} else {
bb =
} this.WriteByte(bb)
} func (this *ByteArray) WriteString(value string){
this.WriteBytes([]byte(value))
} func (this *ByteArray) WriteUTF(value string){
this.WriteInt16(int16(len(value)))
this.WriteBytes([]byte(value))
}
//==========read func (this *ByteArray) Read(bytes []byte) (l int, err error){
if len(bytes) == {
return
}
if len(bytes) > this.Length() - this.posRead{
return , io.EOF
}
l = copy(bytes, this.buf[this.posRead:])
this.posRead += l return l, nil
} func (this *ByteArray) ReadBytes(bytes []byte, length int, offset int) (l int, err error){
return this.Read(bytes[offset:offset + length])
} func (this *ByteArray) ReadByte() (b byte, err error){
bytes := make([]byte, )
_, err = this.ReadBytes(bytes, , ) if err == nil{
b = bytes[]
} return
} func (this *ByteArray) ReadInt8() (ret int8, err error){
err = binary.Read(this, this.endian, &ret)
return
} func (this *ByteArray) ReadInt16() (ret int16, err error){
err = binary.Read(this, this.endian, &ret)
return
} func (this *ByteArray) ReadInt32() (ret int32, err error){
err = binary.Read(this, this.endian, &ret)
return
} func (this *ByteArray) ReadInt64() (ret int64, err error){
err = binary.Read(this, this.endian, &ret)
return
} func (this *ByteArray) ReadFloat32() (ret float32, err error){
err = binary.Read(this, this.endian, &ret)
return
} func (this *ByteArray) ReadFloat64() (ret float64, err error){
err = binary.Read(this, this.endian, &ret)
return
} func (this *ByteArray) ReadBool() (ret bool, err error){
var bb byte
bb, err = this.ReadByte()
if err == nil{
if bb == {
ret = true
} else {
ret = false
}
}else{
ret = false
}
return
} func (this *ByteArray) ReadString(length int) (ret string, err error){
bytes := make([]byte, length)
_, err = this.ReadBytes(bytes, length, )
if err == nil{
ret = string(bytes)
}else{
ret = "";
}
return
} func (this *ByteArray) ReadUTF() (ret string, err error){
var l int16
l, err = this.ReadInt16() if err != nil{
return "", err
} return this.ReadString(int(l))
}

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

我现在已经把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. smarty之缓存笔记

    smarty缓存技术 在smarty中,缓存分为:普通缓存,单模版都缓存,局部缓存. 缓存:1:首选打开缓存配置项:$smarty->caching=true; 2:缓存生命周期的配置选项:$s ...

  2. 在PHP代码中处理JSON 格式的字符串的两种方法:

    总结: 在PHP代码中处理JSON 格式的字符串的两种方法: 方法一: $json= '[{"id":"1","name":"\u ...

  3. Swift笔记2

    1.元组类型 let cat =(age:4,weight:2,cocle:"black",beauty :true) if(cat.beauty){ printf("我 ...

  4. inux网卡与MAC地址绑定方法总结

        使用linux系统时会出现这样的情况,当你安装了某个网卡的驱动程序时,或者安装了与网卡相关的程序后. 网卡会出现所谓的漂移现象.(注意:不是飘逸).可能的表象为: (1):网卡顺序颠倒,比如之 ...

  5. 用实例给新手讲解RSA加密算法

    图为 RSA公开密钥算法的发明人,从左到右Ron Rivest, Adi Shamir, Leonard Adleman. 照片摄于1978年 RSA加密算法是最常用的非对称加密算法,CFCA在证书服 ...

  6. Android开发之发送邮件功能的实现(源代码分享)

    Android开发中可能会碰到如何发送邮件的困扰,之前我也查了相关的文档,博友们也分享了不少的发送邮件的办法,总共有3种把,我仔细阅读了下,发现有的讲的太过复杂跟麻烦,不够清晰,我今天就来分享下我认为 ...

  7. ajax缺点以及解决办法

    1.缺少一个没有标准之争.没有back和history的浏览器    Ajax取消了back按钮,即对浏览器后退机制的破坏.后退按钮是一个标准的web站点的重要功能,但是它没法和js进行很好的合作,这 ...

  8. zk 节点故障 重连机制

    <pre name="code" class="html">如果在连接时候zk服务器宕机 To create a client session th ...

  9. [Leetcode][Python]19: Remove Nth Node From End of List

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 38: Count and Sayhttps://oj.leetcode.co ...

  10. C++ STL 一般总结

    以下内容来源网上 经过整合而成(转载) 一.一般介绍 STL(Standard Template Library),即标准模板库,是一个具有工业强度的,高效的C++程序库.它被容纳于C++标准程序库( ...