util: use more consistent and explicit naming for BR/BW
This commit is contained in:
parent
ad2cd15c6c
commit
672668b9fb
2 changed files with 29 additions and 30 deletions
|
@ -12,27 +12,27 @@ type BinReader struct {
|
||||||
Err error
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read reads from the underlying io.Reader
|
// ReadLE reads from the underlying io.Reader
|
||||||
// into the interface v in LE
|
// into the interface v in little-endian format
|
||||||
func (r *BinReader) Read(v interface{}) {
|
func (r *BinReader) ReadLE(v interface{}) {
|
||||||
if r.Err != nil {
|
if r.Err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.Err = binary.Read(r.R, binary.LittleEndian, v)
|
r.Err = binary.Read(r.R, binary.LittleEndian, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadBigEnd reads from the underlying io.Reader
|
// ReadBE reads from the underlying io.Reader
|
||||||
// into the interface v in BE
|
// into the interface v in big-endian format
|
||||||
func (r *BinReader) ReadBigEnd(v interface{}) {
|
func (r *BinReader) ReadBE(v interface{}) {
|
||||||
if r.Err != nil {
|
if r.Err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.Err = binary.Read(r.R, binary.BigEndian, v)
|
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
|
// underlying reader
|
||||||
func (r *BinReader) VarUint() uint64 {
|
func (r *BinReader) ReadVarUint() uint64 {
|
||||||
var b uint8
|
var b uint8
|
||||||
r.Err = binary.Read(r.R, binary.LittleEndian, &b)
|
r.Err = binary.Read(r.R, binary.LittleEndian, &b)
|
||||||
|
|
||||||
|
@ -55,17 +55,17 @@ func (r *BinReader) VarUint() uint64 {
|
||||||
return uint64(b)
|
return uint64(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// VarBytes reads the next set of bytes from the underlying reader.
|
// ReadBytes reads the next set of bytes from the underlying reader.
|
||||||
// VarUInt is used to determine how large that slice is
|
// ReadVarUInt() is used to determine how large that slice is
|
||||||
func (r *BinReader) VarBytes() []byte {
|
func (r *BinReader) ReadBytes() []byte {
|
||||||
n := r.VarUint()
|
n := r.ReadVarUint()
|
||||||
b := make([]byte, n)
|
b := make([]byte, n)
|
||||||
r.Read(b)
|
r.ReadLE(b)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// VarString calls VarBytes and casts the results as a string
|
// ReadString calls ReadBytes and casts the results as a string
|
||||||
func (r *BinReader) VarString() string {
|
func (r *BinReader) ReadString() string {
|
||||||
b := r.VarBytes()
|
b := r.ReadBytes()
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,25 +14,24 @@ type BinWriter struct {
|
||||||
Err error
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write writes into the underlying io.Writer from an object v in LE format
|
// WriteLE writes into the underlying io.Writer from an object v in little-endian format
|
||||||
func (w *BinWriter) Write(v interface{}) {
|
func (w *BinWriter) WriteLE(v interface{}) {
|
||||||
if w.Err != nil {
|
if w.Err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Err = binary.Write(w.W, binary.LittleEndian, v)
|
w.Err = binary.Write(w.W, binary.LittleEndian, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteBigEnd writes into the underlying io.Writer from an object v in BE format
|
// WriteBE writes into the underlying io.Writer from an object v in big-endian format
|
||||||
// Only used for IP and PORT. Additional method makes the default LittleEndian case clean
|
func (w *BinWriter) WriteBE(v interface{}) {
|
||||||
func (w *BinWriter) WriteBigEnd(v interface{}) {
|
|
||||||
if w.Err != nil {
|
if w.Err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Err = binary.Write(w.W, binary.BigEndian, v)
|
w.Err = binary.Write(w.W, binary.BigEndian, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// VarUint writes a uint64 into the underlying writer
|
// WriteVarUint writes a uint64 into the underlying writer using variable-length encoding
|
||||||
func (w *BinWriter) VarUint(val uint64) {
|
func (w *BinWriter) WriteVarUint(val uint64) {
|
||||||
if val < 0 {
|
if val < 0 {
|
||||||
w.Err = errors.New("value out of range")
|
w.Err = errors.New("value out of range")
|
||||||
return
|
return
|
||||||
|
@ -63,13 +62,13 @@ func (w *BinWriter) VarUint(val uint64) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// VarBytes writes a variable length byte array into the underlying io.Writer
|
// WriteBytes writes a variable length byte array into the underlying io.Writer
|
||||||
func (w *BinWriter) VarBytes(b []byte) {
|
func (w *BinWriter) WriteBytes(b []byte) {
|
||||||
w.VarUint(uint64(len(b)))
|
w.WriteVarUint(uint64(len(b)))
|
||||||
w.Write(b)
|
w.WriteLE(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
//VarString casts the string as a byte slice and calls VarBytes
|
// WriteString writes a variable length string into the underlying io.Writer
|
||||||
func (w *BinWriter) VarString(s string) {
|
func (w *BinWriter) WriteString(s string) {
|
||||||
w.VarBytes([]byte(s))
|
w.WriteBytes([]byte(s))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue