neoneo-go/pkg/io/binaryWriter.go

111 lines
2.8 KiB
Go
Raw Normal View History

package io
2019-02-25 22:44:14 +00:00
import (
"encoding/binary"
"io"
"reflect"
2019-02-25 22:44:14 +00:00
)
2019-10-22 14:56:03 +00:00
// BinWriter is a convenient wrapper around a io.Writer and err object.
// Used to simplify error handling when writing into a io.Writer
2019-10-22 14:56:03 +00:00
// from a struct with many fields.
2019-02-25 22:44:14 +00:00
type BinWriter struct {
w io.Writer
2019-02-25 22:44:14 +00:00
Err error
}
// NewBinWriterFromIO makes a BinWriter from io.Writer.
func NewBinWriterFromIO(iow io.Writer) *BinWriter {
return &BinWriter{w: iow}
}
2019-10-22 14:56:03 +00:00
// WriteLE writes into the underlying io.Writer from an object v in little-endian format.
func (w *BinWriter) WriteLE(v interface{}) {
2019-02-25 22:44:14 +00:00
if w.Err != nil {
return
}
w.Err = binary.Write(w.w, binary.LittleEndian, v)
2019-02-25 22:44:14 +00:00
}
2019-10-22 14:56:03 +00:00
// WriteBE writes into the underlying io.Writer from an object v in big-endian format.
func (w *BinWriter) WriteBE(v interface{}) {
2019-02-25 22:44:14 +00:00
if w.Err != nil {
return
}
w.Err = binary.Write(w.w, binary.BigEndian, v)
2019-02-25 22:44:14 +00:00
}
// WriteArray writes a slice or an array arr into w.
func (w *BinWriter) WriteArray(arr interface{}) {
switch val := reflect.ValueOf(arr); val.Kind() {
case reflect.Slice, reflect.Array:
typ := val.Type().Elem()
method, ok := typ.MethodByName("EncodeBinary")
if !ok || !isEncodeBinaryMethod(method) {
panic(typ.String() + " does not have EncodeBinary(*BinWriter)")
}
if w.Err != nil {
return
}
w.WriteVarUint(uint64(val.Len()))
for i := 0; i < val.Len(); i++ {
method := val.Index(i).MethodByName("EncodeBinary")
method.Call([]reflect.Value{reflect.ValueOf(w)})
}
default:
panic("not an array")
}
}
func isEncodeBinaryMethod(method reflect.Method) bool {
t := method.Type
return t != nil &&
t.NumIn() == 2 && t.In(1) == reflect.TypeOf((*BinWriter)(nil)) &&
t.NumOut() == 0
}
2019-10-22 14:56:03 +00:00
// WriteVarUint writes a uint64 into the underlying writer using variable-length encoding.
func (w *BinWriter) WriteVarUint(val uint64) {
if w.Err != nil {
return
}
2019-02-25 22:44:14 +00:00
if val < 0xfd {
w.Err = binary.Write(w.w, binary.LittleEndian, uint8(val))
2019-02-25 22:44:14 +00:00
return
}
if val < 0xFFFF {
w.Err = binary.Write(w.w, binary.LittleEndian, byte(0xfd))
w.Err = binary.Write(w.w, binary.LittleEndian, uint16(val))
2019-02-25 22:44:14 +00:00
return
}
if val < 0xFFFFFFFF {
w.Err = binary.Write(w.w, binary.LittleEndian, byte(0xfe))
w.Err = binary.Write(w.w, binary.LittleEndian, uint32(val))
2019-02-25 22:44:14 +00:00
return
}
w.Err = binary.Write(w.w, binary.LittleEndian, byte(0xff))
w.Err = binary.Write(w.w, binary.LittleEndian, val)
2019-02-25 22:44:14 +00:00
}
// WriteBytes writes a variable byte into the underlying io.Writer without prefix.
func (w *BinWriter) WriteBytes(b []byte) {
w.WriteLE(b)
}
// WriteVarBytes writes a variable length byte array into the underlying io.Writer.
func (w *BinWriter) WriteVarBytes(b []byte) {
w.WriteVarUint(uint64(len(b)))
w.WriteLE(b)
2019-02-25 22:44:14 +00:00
}
2019-10-22 14:56:03 +00:00
// WriteString writes a variable length string into the underlying io.Writer.
func (w *BinWriter) WriteString(s string) {
w.WriteVarBytes([]byte(s))
}