forked from n10v/id3v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_frame.go
56 lines (43 loc) · 1.15 KB
/
text_frame.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
// Copyright 2016 Albert Nigmatzianov. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package id3v2
import "io"
// TextFrame is used to work with all text frames
// (all T*** frames like TIT2 (title), TALB (album) and so on).
type TextFrame struct {
Encoding Encoding
Text string
}
func (tf TextFrame) Size() int {
return 1 + encodedSize(tf.Text, tf.Encoding)
}
func (tf TextFrame) WriteTo(w io.Writer) (n int64, err error) {
bw := getBufioWriter(w)
defer putBufioWriter(bw)
bw.WriteByte(tf.Encoding.Key)
_, err = encodeWriteText(bw, tf.Text, tf.Encoding)
if err != nil {
return
}
return int64(bw.Buffered()), bw.Flush()
}
func parseTextFrame(rd io.Reader) (Framer, error) {
bufRd := getUtilReader(rd)
defer putUtilReader(bufRd)
encodingKey, err := bufRd.ReadByte()
if err != nil {
return nil, err
}
encoding := getEncoding(encodingKey)
buf := getBytesBuffer()
defer putBytesBuffer(buf)
if _, err := buf.ReadFrom(bufRd); err != nil {
return nil, err
}
tf := TextFrame{
Encoding: encoding,
Text: decodeText(buf.Bytes(), encoding),
}
return tf, nil
}