github.com/annchain/OG@v0.0.9/common/byteutil/binary_writer.go (about)

     1  package byteutil
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"github.com/annchain/OG/arefactor/common/utilfuncs"
     7  )
     8  
     9  type BinaryWriter struct {
    10  	buf *bytes.Buffer
    11  }
    12  
    13  func NewBinaryWriter() *BinaryWriter {
    14  	return &BinaryWriter{
    15  		buf: &bytes.Buffer{},
    16  	}
    17  }
    18  
    19  func (s *BinaryWriter) Write(datas ...interface{}) {
    20  	if s.buf == nil {
    21  		s.buf = &bytes.Buffer{}
    22  	}
    23  	for _, data := range datas {
    24  		utilfuncs.PanicIfError(binary.Write(s.buf, binary.BigEndian, data), "binary writer")
    25  	}
    26  }
    27  
    28  //get bytes
    29  func (s *BinaryWriter) Bytes() []byte {
    30  	return s.buf.Bytes()
    31  }