neo-go/pkg/io/binaryBufWriter.go
Roman Khimov 23f9c5a43b io: specify Bytes() behavior a bit better for BufBinWriter
It's not immediately obvious.

Signed-off-by: Roman Khimov <roman@nspcc.ru>
2024-11-05 16:58:33 +03:00

50 lines
1.4 KiB
Go

package io
import (
"bytes"
"errors"
)
// ErrDrained is returned on an attempt to use an already drained write buffer.
var ErrDrained = errors.New("buffer already drained")
// BufBinWriter is an additional layer on top of BinWriter that
// automatically creates a 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(BufBinWriter)
b.BinWriter = NewBinWriterFromIO(&b.buf)
return b
}
// Len returns the number of bytes of the unread portion of the buffer.
func (bw *BufBinWriter) Len() int {
return bw.buf.Len()
}
// Bytes returns the resulting buffer and makes future writes return an error.
// Subsequent calls to it will return nil. You can reuse this instance of
// [BufBinWriter] after [BufBinWriter.Reset].
func (bw *BufBinWriter) Bytes() []byte {
if bw.Err != nil {
return nil
}
bw.Err = ErrDrained
return bw.buf.Bytes()
}
// Reset resets the state of the buffer, making it usable again. It can
// make buffer usage somewhat more efficient because you don't need to
// create it again. But beware, the buffer is gonna be the same as the one
// returned by Bytes(), so if you need that data after Reset() you have to copy
// it yourself.
func (bw *BufBinWriter) Reset() {
bw.Err = nil
bw.buf.Reset()
}