首页 技术 正文
技术 2022年11月14日
0 收藏 915 点赞 2,219 浏览 5290 个字

用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/

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294