-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwriter.go
71 lines (57 loc) · 1.16 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package enkodo
import "io"
// NewWriter will initialize a new instance of writer
func NewWriter(in io.Writer) *Writer {
var w Writer
w.e = newEncoder(in)
return &w
}
// Writer manages the writing of enkodo output
type Writer struct {
e *Encoder
}
// Encode will encode an encodee
func (w *Writer) Encode(v Encodee) (err error) {
if w.e == nil {
return ErrIsClosed
}
return v.MarshalEnkodo(w.e)
}
// Reset will reset the underlying bytes of the Encoder
func (w *Writer) Reset() {
if w.e == nil {
return
}
w.e.bs = w.e.bs[:0]
}
// WriteTo will write to an io.Writer
func (w *Writer) WriteTo(dest io.Writer) (n int64, err error) {
if w.e == nil {
err = ErrIsClosed
return
}
var written int
if written, err = dest.Write(w.Bytes()); err != nil {
return
}
n = int64(written)
w.Reset()
return
}
// Bytes will expose the underlying bytes
func (w *Writer) Bytes() []byte {
return w.e.bs
}
// Written will return the total number of bytes written
func (w *Writer) Written() int64 {
return w.e.written
}
// Close will close the writer
func (w *Writer) Close() (err error) {
if w.e == nil {
return ErrIsClosed
}
w.e.teardown()
w.e = nil
return
}