github.com/kayoticsully/syncthing@v0.8.9-0.20140724133906-c45a2fdc03f8/xdr/encdec_xdr_test.go (about) 1 package xdr_test 2 3 import ( 4 "bytes" 5 "io" 6 7 "github.com/calmh/syncthing/xdr" 8 ) 9 10 func (o TestStruct) EncodeXDR(w io.Writer) (int, error) { 11 var xw = xdr.NewWriter(w) 12 return o.encodeXDR(xw) 13 } 14 15 func (o TestStruct) MarshalXDR() []byte { 16 var buf bytes.Buffer 17 var xw = xdr.NewWriter(&buf) 18 o.encodeXDR(xw) 19 return buf.Bytes() 20 } 21 22 func (o TestStruct) encodeXDR(xw *xdr.Writer) (int, error) { 23 xw.WriteUint64(uint64(o.I)) 24 xw.WriteUint16(uint16(o.I16)) 25 xw.WriteUint16(o.UI16) 26 xw.WriteUint32(uint32(o.I32)) 27 xw.WriteUint32(o.UI32) 28 xw.WriteUint64(uint64(o.I64)) 29 xw.WriteUint64(o.UI64) 30 xw.WriteBytes(o.BS) 31 xw.WriteString(o.S) 32 return xw.Tot(), xw.Error() 33 } 34 35 func (o *TestStruct) DecodeXDR(r io.Reader) error { 36 xr := xdr.NewReader(r) 37 return o.decodeXDR(xr) 38 } 39 40 func (o *TestStruct) UnmarshalXDR(bs []byte) error { 41 var buf = bytes.NewBuffer(bs) 42 var xr = xdr.NewReader(buf) 43 return o.decodeXDR(xr) 44 } 45 46 func (o *TestStruct) decodeXDR(xr *xdr.Reader) error { 47 o.I = int(xr.ReadUint64()) 48 o.I16 = int16(xr.ReadUint16()) 49 o.UI16 = xr.ReadUint16() 50 o.I32 = int32(xr.ReadUint32()) 51 o.UI32 = xr.ReadUint32() 52 o.I64 = int64(xr.ReadUint64()) 53 o.UI64 = xr.ReadUint64() 54 o.BS = xr.ReadBytes() 55 o.S = xr.ReadString() 56 return xr.Error() 57 }