From 672668b9fbc265a0190d2ac1cf5d904869b8159a Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 28 Aug 2019 13:24:06 +0300 Subject: [PATCH] util: use more consistent and explicit naming for BR/BW --- pkg/util/binaryReader.go | 32 ++++++++++++++++---------------- pkg/util/binaryWriter.go | 27 +++++++++++++-------------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/pkg/util/binaryReader.go b/pkg/util/binaryReader.go index 529610f14..f13879549 100644 --- a/pkg/util/binaryReader.go +++ b/pkg/util/binaryReader.go @@ -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) } diff --git a/pkg/util/binaryWriter.go b/pkg/util/binaryWriter.go index d4fa34ae6..39acf9a41 100644 --- a/pkg/util/binaryWriter.go +++ b/pkg/util/binaryWriter.go @@ -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)) }