forked from mikkyang/id3-go
/
writer.go
63 lines (52 loc) · 1.2 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
// Copyright 2013 Michael Yang. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package encodedbytes
import (
"bytes"
"io"
)
// Writer is a helper for writing frame bytes
type Writer struct {
data []byte
index int // current writing index
}
func (w *Writer) Write(b []byte) (n int, err error) {
if len(b) == 0 {
return 0, nil
}
if w.index >= len(w.data) {
return 0, io.EOF
}
n = copy(w.data[w.index:], b)
w.index += n
return
}
func (w *Writer) WriteByte(b byte) (err error) {
if w.index >= len(w.data) {
return io.EOF
}
w.data[w.index] = b
w.index++
return
}
func (w *Writer) WriteString(s string, encoding byte) (err error) {
encodedString, err := Encoders[encoding].ConvertString(s)
if err != nil {
return err
}
_, err = w.Write([]byte(encodedString))
if err != nil {
return err
}
return
}
func (w *Writer) WriteNullTermString(s string, encoding byte) (err error) {
if err = w.WriteString(s, encoding); err != nil {
return
}
nullLength := EncodingNullLengthForIndex(encoding)
_, err = w.Write(bytes.Repeat([]byte{0x0}, nullLength))
return
}
func NewWriter(b []byte) *Writer { return &Writer{b, 0} }