View Javadoc

1   /*
2    * Entagged Audio Tag library
3    * Copyright (c) 2003-2005 Raphaël Slinckx <raphael@slinckx.net>
4    * 
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *  
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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  	//Compute the number of bytes the tag will be.
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  	//This method is always called with a compatible tag, as returned from getCompatibleTag()
78  	protected abstract int getFixedTagLength(Tag tag) throws UnsupportedEncodingException;
79  	
80  	protected abstract Tag getCompatibleTag(Tag tag);
81  	
82  	//This method is always called with a compatible tag, as returned from getCompatibleTag()
83  	protected abstract void create(Tag tag, ByteBuffer buf, List fields, int tagSize, int padding) throws UnsupportedEncodingException;
84  }