-
Notifications
You must be signed in to change notification settings - Fork 52
/
buf_writer.go
93 lines (78 loc) · 1.51 KB
/
buf_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package id3v2
import (
"bufio"
"io"
)
// bufWriter is used for convenient writing of frames.
type bufWriter struct {
err error
w *bufio.Writer
written int
}
func newBufWriter(w io.Writer) *bufWriter {
return &bufWriter{w: bufio.NewWriter(w)}
}
func (bw *bufWriter) EncodeAndWriteText(src string, to Encoding) {
if bw.err != nil {
return
}
bw.err = encodeWriteText(bw, src, to)
}
func (bw *bufWriter) Flush() error {
if bw.err != nil {
return bw.err
}
return bw.w.Flush()
}
func (bw *bufWriter) Reset(w io.Writer) {
bw.err = nil
bw.written = 0
bw.w.Reset(w)
}
func (bw *bufWriter) WriteByte(c byte) {
if bw.err != nil {
return
}
bw.err = bw.w.WriteByte(c)
if bw.err == nil {
bw.written++
}
}
func (bw *bufWriter) WriteBytesSize(size uint, synchSafe bool) {
if bw.err != nil {
return
}
bw.err = writeBytesSize(bw, size, synchSafe)
}
func (bw *bufWriter) WriteString(s string) {
if bw.err != nil {
return
}
var n int
n, bw.err = bw.w.WriteString(s)
bw.written += n
}
func (bw *bufWriter) Write(p []byte) (n int, err error) {
if bw.err != nil {
return 0, bw.err
}
n, err = bw.w.Write(p)
bw.written += n
bw.err = err
return n, err
}
func (bw *bufWriter) Written() int {
return bw.written
}
func useBufWriter(w io.Writer, f func(*bufWriter)) (int64, error) {
var writtenBefore int
bw, ok := w.(*bufWriter)
if ok {
writtenBefore = bw.Written()
} else {
bw = getBufWriter(w)
defer putBufWriter(bw)
}
f(bw)
return int64(bw.Written() - writtenBefore), bw.Flush()
}