neo-go/pkg/util/binaryBufWriter.go
Roman Khimov aacf58c9ab util: add 'constructors' for BinReader/BinWriter
And an additional BufBinWriter to ease buffer management.
2019-09-16 23:38:48 +03:00

29 lines
687 B
Go

package util
import (
"bytes"
"errors"
)
// BufBinWriter is an additional layer on top of BinWriter that
// automatically creates buffer to write into that you can get after all
// writes via Bytes().
type BufBinWriter struct {
*BinWriter
buf *bytes.Buffer
}
// NewBufBinWriter makes a BufBinWriter with an empty byte buffer.
func NewBufBinWriter() *BufBinWriter {
b := new(bytes.Buffer)
return &BufBinWriter{BinWriter: NewBinWriterFromIO(b), buf: b}
}
// Bytes returns resulting buffer and makes future writes return an error.
func (bw *BufBinWriter) Bytes() []byte {
if bw.Err != nil {
return nil
}
bw.Err = errors.New("buffer already drained")
return bw.buf.Bytes()
}