util: use more consistent and explicit naming for BR/BW

This commit is contained in:
Roman Khimov 2019-08-28 13:24:06 +03:00
parent ad2cd15c6c
commit 672668b9fb
2 changed files with 29 additions and 30 deletions

View file

@ -12,27 +12,27 @@ type BinReader struct {
Err error
}
// Read reads from the underlying io.Reader
// into the interface v in LE
func (r *BinReader) Read(v interface{}) {
// ReadLE reads from the underlying io.Reader
// into the interface v in little-endian format
func (r *BinReader) ReadLE(v interface{}) {
if r.Err != nil {
return
}
r.Err = binary.Read(r.R, binary.LittleEndian, v)
}
// ReadBigEnd reads from the underlying io.Reader
// into the interface v in BE
func (r *BinReader) ReadBigEnd(v interface{}) {
// ReadBE reads from the underlying io.Reader
// into the interface v in big-endian format
func (r *BinReader) ReadBE(v interface{}) {
if r.Err != nil {
return
}
r.Err = binary.Read(r.R, binary.BigEndian, v)
}
//VarUint reads a variable integer from the
// ReadVarUint reads a variable-length-encoded integer from the
// underlying reader
func (r *BinReader) VarUint() uint64 {
func (r *BinReader) ReadVarUint() uint64 {
var b uint8
r.Err = binary.Read(r.R, binary.LittleEndian, &b)
@ -55,17 +55,17 @@ func (r *BinReader) VarUint() uint64 {
return uint64(b)
}
// VarBytes reads the next set of bytes from the underlying reader.
// VarUInt is used to determine how large that slice is
func (r *BinReader) VarBytes() []byte {
n := r.VarUint()
// ReadBytes reads the next set of bytes from the underlying reader.
// ReadVarUInt() is used to determine how large that slice is
func (r *BinReader) ReadBytes() []byte {
n := r.ReadVarUint()
b := make([]byte, n)
r.Read(b)
r.ReadLE(b)
return b
}
// VarString calls VarBytes and casts the results as a string
func (r *BinReader) VarString() string {
b := r.VarBytes()
// ReadString calls ReadBytes and casts the results as a string
func (r *BinReader) ReadString() string {
b := r.ReadBytes()
return string(b)
}

View file

@ -14,25 +14,24 @@ type BinWriter struct {
Err error
}
// Write writes into the underlying io.Writer from an object v in LE format
func (w *BinWriter) Write(v interface{}) {
// WriteLE writes into the underlying io.Writer from an object v in little-endian format
func (w *BinWriter) WriteLE(v interface{}) {
if w.Err != nil {
return
}
w.Err = binary.Write(w.W, binary.LittleEndian, v)
}
// WriteBigEnd writes into the underlying io.Writer from an object v in BE format
// Only used for IP and PORT. Additional method makes the default LittleEndian case clean
func (w *BinWriter) WriteBigEnd(v interface{}) {
// WriteBE writes into the underlying io.Writer from an object v in big-endian format
func (w *BinWriter) WriteBE(v interface{}) {
if w.Err != nil {
return
}
w.Err = binary.Write(w.W, binary.BigEndian, v)
}
// VarUint writes a uint64 into the underlying writer
func (w *BinWriter) VarUint(val uint64) {
// WriteVarUint writes a uint64 into the underlying writer using variable-length encoding
func (w *BinWriter) WriteVarUint(val uint64) {
if val < 0 {
w.Err = errors.New("value out of range")
return
@ -63,13 +62,13 @@ func (w *BinWriter) VarUint(val uint64) {
}
// VarBytes writes a variable length byte array into the underlying io.Writer
func (w *BinWriter) VarBytes(b []byte) {
w.VarUint(uint64(len(b)))
w.Write(b)
// WriteBytes writes a variable length byte array into the underlying io.Writer
func (w *BinWriter) WriteBytes(b []byte) {
w.WriteVarUint(uint64(len(b)))
w.WriteLE(b)
}
//VarString casts the string as a byte slice and calls VarBytes
func (w *BinWriter) VarString(s string) {
w.VarBytes([]byte(s))
// WriteString writes a variable length string into the underlying io.Writer
func (w *BinWriter) WriteString(s string) {
w.WriteBytes([]byte(s))
}