2014-06-01 20:50:14 +00:00
|
|
|
// Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-02-15 11:08:55 +00:00
|
|
|
package xdr
|
|
|
|
|
2014-05-28 10:30:47 +00:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
)
|
2014-02-15 11:08:55 +00:00
|
|
|
|
|
|
|
func pad(l int) int {
|
|
|
|
d := l % 4
|
|
|
|
if d == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 4 - d
|
|
|
|
}
|
|
|
|
|
|
|
|
var padBytes = []byte{0, 0, 0}
|
|
|
|
|
|
|
|
type Writer struct {
|
2014-05-28 10:30:47 +00:00
|
|
|
w io.Writer
|
|
|
|
tot int
|
|
|
|
err error
|
|
|
|
b [8]byte
|
|
|
|
last time.Time
|
2014-02-15 11:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewWriter(w io.Writer) *Writer {
|
|
|
|
return &Writer{
|
|
|
|
w: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Writer) WriteString(s string) (int, error) {
|
|
|
|
return w.WriteBytes([]byte(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Writer) WriteBytes(bs []byte) (int, error) {
|
|
|
|
if w.err != nil {
|
|
|
|
return 0, w.err
|
|
|
|
}
|
|
|
|
|
2014-05-28 10:30:47 +00:00
|
|
|
w.last = time.Now()
|
2014-02-15 11:08:55 +00:00
|
|
|
w.WriteUint32(uint32(len(bs)))
|
|
|
|
if w.err != nil {
|
|
|
|
return 0, w.err
|
|
|
|
}
|
|
|
|
|
2014-05-17 06:43:01 +00:00
|
|
|
if debug {
|
|
|
|
if len(bs) > maxDebugBytes {
|
|
|
|
dl.Debugf("wr bytes (%d): %x...", len(bs), bs[:maxDebugBytes])
|
|
|
|
} else {
|
|
|
|
dl.Debugf("wr bytes (%d): %x", len(bs), bs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 11:08:55 +00:00
|
|
|
var l, n int
|
|
|
|
n, w.err = w.w.Write(bs)
|
|
|
|
l += n
|
|
|
|
|
|
|
|
if p := pad(len(bs)); w.err == nil && p > 0 {
|
|
|
|
n, w.err = w.w.Write(padBytes[:p])
|
|
|
|
l += n
|
|
|
|
}
|
|
|
|
|
2014-02-20 16:40:15 +00:00
|
|
|
w.tot += l
|
|
|
|
return l, w.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Writer) WriteUint16(v uint16) (int, error) {
|
|
|
|
if w.err != nil {
|
|
|
|
return 0, w.err
|
|
|
|
}
|
2014-05-17 06:43:01 +00:00
|
|
|
|
2014-05-28 10:30:47 +00:00
|
|
|
w.last = time.Now()
|
2014-05-17 06:43:01 +00:00
|
|
|
if debug {
|
|
|
|
dl.Debugf("wr uint16=%d", v)
|
|
|
|
}
|
|
|
|
|
2014-02-20 16:40:15 +00:00
|
|
|
w.b[0] = byte(v >> 8)
|
|
|
|
w.b[1] = byte(v)
|
|
|
|
w.b[2] = 0
|
|
|
|
w.b[3] = 0
|
|
|
|
|
|
|
|
var l int
|
|
|
|
l, w.err = w.w.Write(w.b[:4])
|
|
|
|
w.tot += l
|
2014-02-15 11:08:55 +00:00
|
|
|
return l, w.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Writer) WriteUint32(v uint32) (int, error) {
|
|
|
|
if w.err != nil {
|
|
|
|
return 0, w.err
|
|
|
|
}
|
2014-05-17 06:43:01 +00:00
|
|
|
|
2014-05-28 10:30:47 +00:00
|
|
|
w.last = time.Now()
|
2014-05-17 06:43:01 +00:00
|
|
|
if debug {
|
|
|
|
dl.Debugf("wr uint32=%d", v)
|
|
|
|
}
|
|
|
|
|
2014-02-15 11:08:55 +00:00
|
|
|
w.b[0] = byte(v >> 24)
|
|
|
|
w.b[1] = byte(v >> 16)
|
|
|
|
w.b[2] = byte(v >> 8)
|
|
|
|
w.b[3] = byte(v)
|
|
|
|
|
|
|
|
var l int
|
|
|
|
l, w.err = w.w.Write(w.b[:4])
|
2014-02-20 16:40:15 +00:00
|
|
|
w.tot += l
|
2014-02-15 11:08:55 +00:00
|
|
|
return l, w.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Writer) WriteUint64(v uint64) (int, error) {
|
|
|
|
if w.err != nil {
|
|
|
|
return 0, w.err
|
|
|
|
}
|
2014-05-17 06:43:01 +00:00
|
|
|
|
2014-05-28 10:30:47 +00:00
|
|
|
w.last = time.Now()
|
2014-05-17 06:43:01 +00:00
|
|
|
if debug {
|
|
|
|
dl.Debugf("wr uint64=%d", v)
|
|
|
|
}
|
|
|
|
|
2014-02-15 11:08:55 +00:00
|
|
|
w.b[0] = byte(v >> 56)
|
|
|
|
w.b[1] = byte(v >> 48)
|
|
|
|
w.b[2] = byte(v >> 40)
|
|
|
|
w.b[3] = byte(v >> 32)
|
|
|
|
w.b[4] = byte(v >> 24)
|
|
|
|
w.b[5] = byte(v >> 16)
|
|
|
|
w.b[6] = byte(v >> 8)
|
|
|
|
w.b[7] = byte(v)
|
|
|
|
|
|
|
|
var l int
|
|
|
|
l, w.err = w.w.Write(w.b[:8])
|
2014-02-20 16:40:15 +00:00
|
|
|
w.tot += l
|
2014-02-15 11:08:55 +00:00
|
|
|
return l, w.err
|
|
|
|
}
|
|
|
|
|
2014-02-20 16:40:15 +00:00
|
|
|
func (w *Writer) Tot() int {
|
2014-02-15 11:08:55 +00:00
|
|
|
return w.tot
|
|
|
|
}
|
|
|
|
|
2014-02-20 16:40:15 +00:00
|
|
|
func (w *Writer) Error() error {
|
2014-02-15 11:08:55 +00:00
|
|
|
return w.err
|
|
|
|
}
|
2014-05-28 10:30:47 +00:00
|
|
|
|
|
|
|
func (w *Writer) LastWrite() time.Time {
|
|
|
|
return w.last
|
|
|
|
}
|