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.ogg.util;
20  
21  import entagged.audioformats.generic.AbstractTagCreator;
22  import entagged.audioformats.generic.Utils;
23  import entagged.audioformats.ogg.*;
24  import entagged.audioformats.*;
25  
26  import java.io.UnsupportedEncodingException;
27  import java.nio.*;
28  import java.util.*;
29  
30  public class OggTagCreator extends AbstractTagCreator {
31  	
32  	//Creates the ByteBuffer for the ogg tag
33  	public void create(Tag tag, ByteBuffer buf, List fields, int tagSize, int padding) throws UnsupportedEncodingException {
34  		String vendorString = ((OggTag)tag).getVendor();
35  		int vendorLength = Utils.getUTF8Bytes(vendorString).length;
36  		buf.put( new byte[]{(byte)(vendorLength&0xFF), (byte)((vendorLength & 0xFF00) >> 8) , (byte)((vendorLength & 0xFF0000) >> 16), (byte)((vendorLength & 0xFF000000) >> 24)  } );
37  		buf.put( Utils.getUTF8Bytes(vendorString ) );
38  
39  		//[user comment list length]
40  		int listLength = fields.size();
41  		byte[] b = new byte[4];
42  		b[3] = (byte) ( ( listLength & 0xFF000000 ) >> 24 );
43  		b[2] = (byte) ( ( listLength & 0x00FF0000 ) >> 16 );
44  		b[1] = (byte) ( ( listLength & 0x0000FF00 ) >> 8 );
45  		b[0] = (byte) ( listLength & 0x000000FF );
46  		buf.put( b );
47  
48  		Iterator it = fields.iterator();
49  		while(it.hasNext()) {
50  			buf.put((byte[]) it.next());
51  		}
52  	}
53  	
54  	protected Tag getCompatibleTag(Tag tag) {
55  	    if(! (tag instanceof OggTag)) {
56  		    OggTag oggTag = new OggTag();
57  		    oggTag.merge(tag);
58  		    return oggTag;
59  		}
60  		return tag;
61  	}
62  	
63  	protected int getFixedTagLength(Tag tag) throws UnsupportedEncodingException {
64  		return 8 + Utils.getUTF8Bytes(((OggTag)tag).getVendor()).length;
65  	}
66  }