1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package entagged.audioformats.mp3.util.id3frames;
20
21 import java.io.UnsupportedEncodingException;
22
23 import entagged.audioformats.generic.TagField;
24 import entagged.audioformats.generic.TagTextField;
25 import entagged.audioformats.mp3.Id3v2Tag;
26
27 public class TextId3Frame extends Id3Frame implements TagTextField {
28
29 protected String content;
30
31 protected byte encoding;
32
33 protected String id;
34
35 protected boolean common;
36
37
38
39
40
41 public TextId3Frame(String id, String content) {
42 this.id = id;
43 checkCommon();
44 this.content = content;
45 setEncoding(Id3v2Tag.DEFAULT_ENCODING);
46 }
47
48 public TextId3Frame(String id, byte[] rawContent, byte version)
49 throws UnsupportedEncodingException {
50 super(rawContent, version);
51 this.id = id;
52 checkCommon();
53 }
54
55 private void checkCommon() {
56
57 this.common = id.equals("TIT2") || id.equals("TALB")
58 || id.equals("TPE1") || id.equals("TCON") || id.equals("TRCK")
59 || id.equals("TDRC") || id.equals("COMM");
60 }
61
62 public String getEncoding() {
63 if (encoding == 0)
64 return "ISO-8859-1";
65 else if (encoding == 1)
66 return "UTF-16";
67 else if (encoding == 2)
68 return "UTF-16BE";
69 else if (encoding == 3) {
70 return "UTF-8";
71 }
72 return "ISO-8859-1";
73 }
74
75 public void setEncoding(String enc) {
76 if ("ISO-8859-1".equals(enc))
77 encoding = 0;
78 else if ("UTF-16".equals(enc))
79 encoding = 1;
80 else if ("UTF-16BE".equals(enc))
81 encoding = 2;
82 else if ("UTF-8".equals(enc))
83 encoding = 3;
84 else
85 encoding = 1;
86 }
87
88 public String getContent() {
89 return content;
90 }
91
92 public boolean isBinary() {
93 return false;
94 }
95
96 public String getId() {
97 return this.id;
98 }
99
100 public boolean isCommon() {
101 return this.common;
102 }
103
104 public void setContent(String s) {
105 this.content = s;
106 }
107
108 public boolean isEmpty() {
109 return this.content.equals("");
110 }
111
112 public void copyContent(TagField field) {
113 if (field instanceof TextId3Frame) {
114 this.content = ((TextId3Frame) field).getContent();
115 setEncoding(((TextId3Frame) field).getEncoding());
116 }
117 }
118
119 protected void populate(byte[] raw) throws UnsupportedEncodingException {
120 this.encoding = raw[flags.length];
121 if (this.encoding != 0 && this.encoding != 1 && this.encoding != 2
122 && this.encoding != 3)
123 this.encoding = 0;
124
125 this.content = getString(raw, flags.length + 1, raw.length
126 - flags.length - 1, getEncoding());
127
128 int i = this.content.indexOf("\u0000");
129
130 if (i != -1)
131 this.content = this.content.substring(0, i);
132 }
133
134 protected byte[] build() throws UnsupportedEncodingException {
135 byte[] data = getBytes(this.content, getEncoding());
136
137 byte[] b = new byte[4 + 4 + flags.length + 1 + data.length];
138
139 int offset = 0;
140 copy(getIdBytes(), b, offset);
141 offset += 4;
142 copy(getSize(b.length - 10), b, offset);
143 offset += 4;
144 copy(flags, b, offset);
145 offset += flags.length;
146
147 b[offset] = this.encoding;
148 offset += 1;
149
150 copy(data, b, offset);
151
152 return b;
153 }
154
155 public String toString() {
156 return getContent();
157 }
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189 }