| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- package public
- import (
- "encoding/binary"
- "io"
- )
- type ByteArray struct {
- buf []byte
- posWrite int
- posRead int
- endian binary.ByteOrder
- }
- var ByteArrayEndian binary.ByteOrder = binary.LittleEndian
- func CreateByteArray(bytes []byte) *ByteArray {
- var ba *ByteArray
- if bytes != nil && len(bytes) > 0 {
- ba = &ByteArray{buf: bytes}
- } else {
- ba = &ByteArray{}
- }
- ba.endian = binary.LittleEndian
- 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 == 0 {
- return
- }
- space := len(this.buf) - this.posWrite
- if space >= l {
- return
- }
- needGrow := l - space
- bufGrow := make([]byte, needGrow)
- this.buf = append(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(0)
- }
- 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) error {
- bytes := make([]byte, 1)
- bytes[0] = b
- _, err := this.WriteBytes(bytes)
- return err
- }
- func (this *ByteArray) WriteInt8(value int8) {
- binary.Write(this, this.endian, &value)
- }
- func (this *ByteArray) WriteUInt8(value uint8) {
- binary.Write(this, this.endian, &value)
- }
- func (this *ByteArray) WriteInt16(value int16) {
- binary.Write(this, this.endian, &value)
- }
- func (this *ByteArray) WriteUInt16(value uint16) {
- binary.Write(this, this.endian, &value)
- }
- func (this *ByteArray) WriteInt32(value int32) {
- binary.Write(this, this.endian, &value)
- }
- func (this *ByteArray) WriteUInt32(value uint32) {
- binary.Write(this, this.endian, &value)
- }
- /*func (this *ByteArray) WriteInt(value int) {
- binary.Write(this, this.endian, &value)
- }
- func (this *ByteArray) WriteUInt(value uint) {
- 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 = 1
- } else {
- bb = 0
- }
- this.WriteByte(bb)
- }
- func (this *ByteArray) WriteString(value string) {
- this.WriteBytes([]byte(value))
- }
- func (this *ByteArray) WriteStringWithLen(value string, stringLen int) {
- byString := []byte(value)
- this.WriteBytes(byString)
- for i := 0; i < stringLen-len(byString); i++ {
- this.WriteUInt8(0)
- }
- }
- 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) == 0 {
- return
- }
- if len(bytes) > this.Length()-this.posRead {
- return 0, 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, 1)
- _, err = this.ReadBytes(bytes, 1, 0)
- if err == nil {
- b = bytes[0]
- }
- return
- }
- func (this *ByteArray) ReadInt8() (ret int8, err error) {
- err = binary.Read(this, this.endian, &ret)
- return
- }
- func (this *ByteArray) ReadUInt8() (ret uint8, 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) ReadUInt16() (ret uint16, 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) ReadUInt32() (ret uint32, 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 == 1 {
- 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, 0)
- if err == nil {
- // 有可能有末尾的多余空格
- for i := 0; i < length; i++ {
- if bytes[i] == 0 {
- bytes = bytes[:i]
- break
- }
- }
- 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))
- }
|