github.com/kayoticsully/syncthing@v0.8.9-0.20140724133906-c45a2fdc03f8/xdr/bench_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 XDRBenchStruct) EncodeXDR(w io.Writer) (int, error) { 11 var xw = xdr.NewWriter(w) 12 return o.encodeXDR(xw) 13 } 14 15 func (o XDRBenchStruct) MarshalXDR() []byte { 16 var aw = make(xdr.AppendWriter, 0, 128) 17 var xw = xdr.NewWriter(&aw) 18 o.encodeXDR(xw) 19 return []byte(aw) 20 } 21 22 func (o XDRBenchStruct) encodeXDR(xw *xdr.Writer) (int, error) { 23 xw.WriteUint64(o.I1) 24 xw.WriteUint32(o.I2) 25 xw.WriteUint16(o.I3) 26 if len(o.Bs0) > 128 { 27 return xw.Tot(), xdr.ErrElementSizeExceeded 28 } 29 xw.WriteBytes(o.Bs0) 30 xw.WriteBytes(o.Bs1) 31 if len(o.S0) > 128 { 32 return xw.Tot(), xdr.ErrElementSizeExceeded 33 } 34 xw.WriteString(o.S0) 35 xw.WriteString(o.S1) 36 return xw.Tot(), xw.Error() 37 } 38 39 func (o *XDRBenchStruct) DecodeXDR(r io.Reader) error { 40 xr := xdr.NewReader(r) 41 return o.decodeXDR(xr) 42 } 43 44 func (o *XDRBenchStruct) UnmarshalXDR(bs []byte) error { 45 var br = bytes.NewReader(bs) 46 var xr = xdr.NewReader(br) 47 return o.decodeXDR(xr) 48 } 49 50 func (o *XDRBenchStruct) decodeXDR(xr *xdr.Reader) error { 51 o.I1 = xr.ReadUint64() 52 o.I2 = xr.ReadUint32() 53 o.I3 = xr.ReadUint16() 54 o.Bs0 = xr.ReadBytesMax(128) 55 o.Bs1 = xr.ReadBytes() 56 o.S0 = xr.ReadStringMax(128) 57 o.S1 = xr.ReadString() 58 return xr.Error() 59 } 60 61 func (o repeatReader) EncodeXDR(w io.Writer) (int, error) { 62 var xw = xdr.NewWriter(w) 63 return o.encodeXDR(xw) 64 } 65 66 func (o repeatReader) MarshalXDR() []byte { 67 var aw = make(xdr.AppendWriter, 0, 128) 68 var xw = xdr.NewWriter(&aw) 69 o.encodeXDR(xw) 70 return []byte(aw) 71 } 72 73 func (o repeatReader) encodeXDR(xw *xdr.Writer) (int, error) { 74 xw.WriteBytes(o.data) 75 return xw.Tot(), xw.Error() 76 } 77 78 func (o *repeatReader) DecodeXDR(r io.Reader) error { 79 xr := xdr.NewReader(r) 80 return o.decodeXDR(xr) 81 } 82 83 func (o *repeatReader) UnmarshalXDR(bs []byte) error { 84 var br = bytes.NewReader(bs) 85 var xr = xdr.NewReader(br) 86 return o.decodeXDR(xr) 87 } 88 89 func (o *repeatReader) decodeXDR(xr *xdr.Reader) error { 90 o.data = xr.ReadBytes() 91 return xr.Error() 92 }