1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package entagged.audioformats.generic;
20
21 import java.io.UnsupportedEncodingException;
22 import java.nio.ByteBuffer;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
26
27 import entagged.audioformats.Tag;
28
29 public abstract class AbstractTagCreator {
30
31 public ByteBuffer convert(Tag tag) throws UnsupportedEncodingException {
32 return convert(tag, 0);
33 }
34
35 public ByteBuffer convert(Tag tag, int padding) throws UnsupportedEncodingException {
36 Tag compatibleTag = getCompatibleTag(tag);
37
38 List fields = createFields(compatibleTag);
39 int tagSize = computeTagLength(compatibleTag, fields);
40
41 ByteBuffer buf = ByteBuffer.allocate( tagSize + padding );
42 create(compatibleTag, buf, fields, tagSize, padding);
43
44 buf.rewind();
45 return buf;
46 }
47
48 protected List createFields(Tag tag) throws UnsupportedEncodingException {
49 List fields = new LinkedList();
50
51 Iterator it = tag.getFields();
52 while(it.hasNext()) {
53 TagField frame = (TagField) it.next();
54 fields.add(frame.getRawContent());
55 }
56
57 return fields;
58 }
59
60
61 protected int computeTagLength(Tag tag, List l) throws UnsupportedEncodingException {
62 int length = getFixedTagLength(tag);
63
64 Iterator it = l.iterator();
65 while(it.hasNext())
66 length += ((byte[])it.next()).length;
67
68 return length;
69 }
70
71 public int getTagLength(Tag tag) throws UnsupportedEncodingException {
72 Tag compatibleTag = getCompatibleTag(tag);
73 List fields = createFields(compatibleTag);
74 return computeTagLength(compatibleTag, fields);
75 }
76
77
78 protected abstract int getFixedTagLength(Tag tag) throws UnsupportedEncodingException;
79
80 protected abstract Tag getCompatibleTag(Tag tag);
81
82
83 protected abstract void create(Tag tag, ByteBuffer buf, List fields, int tagSize, int padding) throws UnsupportedEncodingException;
84 }