go的一個用於讀寫byte的NetStream

來源:互聯網
上載者:User
網路通訊中通常需要用到byte的通用操作方式 功能大概 寫入int16,int32,int64,byte 讀取int16,int32,int64,byte
我這裡主要做了這些,如果後面需要在進行添加吧
package mynetimport (    "bytes"    "encoding/binary"    "errors")type NetStream struct {    Data     []byte    Rpos     int    Wpos     int    Capacity int}func NewNetStream(capacity int) *NetStream {    a := &NetStream{        Rpos:     0,        Wpos:     0,        Capacity: capacity,    }    a.Data = make([]byte, capacity)    return a}// readfunc (self *NetStream) ReadInt16() (int16, error) {    var p int16    if self.Rpos+2 > self.Wpos {        return p, errors.New("not enough read")    }    binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p)    self.Rpos += 2    return p, nil}func (self *NetStream) ReadInt32() (int32, error) {    var p int32    if self.Rpos+4 > self.Wpos {        return p, errors.New("not enough read")    }    binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p)    self.Rpos += 4    return p, nil}func (self *NetStream) GetInt16() (int16, error) {    var p int16    if self.Rpos+2 > self.Wpos {        return p, errors.New("not enough read")    }    binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p)    return p, nil}func (self *NetStream) GetInt32() (int32, error) {    var p int32    if self.Rpos+4 > self.Wpos {        return p, errors.New("not enough read")    }    binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p)    return p, nil}func (self *NetStream) ReadInt64() (int64, error) {    var p int64    if self.Rpos+8 > self.Wpos {        return p, errors.New("not enough read")    }    binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p)    self.Rpos += 8    return p, nil}func (self *NetStream) ReadBytes(p []byte, offset int, len int) error {    if self.Rpos+len > self.Wpos {        return errors.New("not enough read")    }    copy(p[offset:offset+len], self.Data[self.Rpos:self.Rpos+len])    self.Rpos += len    return nil}// writefunc (self *NetStream) WriteInt16(p int16) error {    if self.Wpos+2 > self.Capacity {        return errors.New("not cap to write")    }    buff := bytes.NewBuffer([]byte{})    binary.Write(buff, binary.BigEndian, p)    self.WriteByte(buff.Bytes())    return nil}func (self *NetStream) WriteInt32(p int32) error {    if self.Wpos+4 > self.Capacity {        return errors.New("not cap to write")    }    buff := bytes.NewBuffer([]byte{})    binary.Write(buff, binary.BigEndian, p)    self.WriteByte(buff.Bytes())    return nil}func (self *NetStream) WriteInt64(p int64) error {    if self.Wpos+8 > self.Capacity {        return errors.New("not cap to write")    }    buff := bytes.NewBuffer([]byte{})    binary.Write(buff, binary.BigEndian, p)    self.WriteByte(buff.Bytes())    return nil}func (self *NetStream) WriteBytes(p []byte, offset int, len int) error {    if self.Wpos+len > self.Capacity {        return errors.New("not cap to write")    }    copy(self.Data[self.Wpos:], p[offset:offset+len])    self.Wpos += len    return nil}func (self *NetStream) WriteByte(p []byte) error {    if self.Wpos+len(p) > self.Capacity {        return errors.New("not cap to write")    }    copy(self.Data[self.Wpos:], p)    self.Wpos += len(p)    return nil}func (self *NetStream) ClearRead() {    if self.Rpos > 0 {        copy(self.Data[0:], self.Data[self.Rpos:self.Wpos])        self.Rpos = 0        self.Wpos = self.Wpos - self.Rpos    }}func (self *NetStream) AvailableNum() int {    return self.Wpos - self.Rpos}func (self *NetStream) PrefixedDataAvailable(len int) bool {    if self.AvailableNum() < len {        return false    }    var b1 int16    var b2 int32    var err error    if len == 2 {        b1, err = self.GetInt16()    } else if len == 4 {        b2, err = self.GetInt32()    }    if err == nil {        if self.AvailableNum() >= int(b1)+len || self.AvailableNum() >= int(b2)+len {            return true        }    }    return false}func (self *NetStream) PrefixedDataAvailableShort() bool {    return self.PrefixedDataAvailable(2)}func (self *NetStream) PrefixedDataAvailableInt() bool {    return self.PrefixedDataAvailable(4)}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.