github.com/contiv/libOpenflow@v0.0.0-20210609050114-d967b14cc688/util/util.go (about)

     1  package util
     2  
     3  import (
     4  	"bytes"
     5  )
     6  
     7  type Message interface {
     8  	//encoding.BinaryMarshaler
     9  	//encoding.BinaryUnmarshaler
    10  	MarshalBinary() (data []byte, err error)
    11  	UnmarshalBinary(data []byte) error
    12  
    13  	Len() uint16
    14  }
    15  
    16  type Buffer struct{ bytes.Buffer }
    17  
    18  func NewBuffer(buf []byte) *Buffer {
    19  	b := new(Buffer)
    20  	b.Buffer = *bytes.NewBuffer(buf)
    21  	return b
    22  }
    23  
    24  func (b *Buffer) Len() uint16 {
    25  	return uint16(b.Buffer.Len())
    26  }
    27  
    28  func (b *Buffer) MarshalBinary() (data []byte, err error) {
    29  	return b.Buffer.Bytes(), nil
    30  }
    31  
    32  func (b *Buffer) UnmarshalBinary(data []byte) error {
    33  	b.Buffer.Reset()
    34  	_, err := b.Buffer.Write(data)
    35  	return err
    36  }