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.ape.util;
20  
21  import entagged.audioformats.ape.ApeTag;
22  import entagged.audioformats.generic.AbstractTagCreator;
23  import entagged.audioformats.*;
24  
25  import java.nio.ByteBuffer;
26  import java.io.UnsupportedEncodingException;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  public class ApeTagCreator extends AbstractTagCreator {
31  		
32  	public void create(Tag tag, ByteBuffer buf, List fields, int tagSize, int paddingSize) throws UnsupportedEncodingException {
33  		byte[] b;
34  
35  		//APETAGEX------------------
36  		buf.put( "APETAGEX".getBytes() );  //APETAGEX
37  		
38  		//Version 2.0 (aka 2000)
39  		buf.put( new byte[] {(byte)0xD0,0x07,0x00,0x00} );
40  		
41  		//Tag size
42  		int size = tagSize - 32;
43  		b = new byte[4];
44  		b[3] = (byte) ( ( size & 0xFF000000 ) >>> 24 );
45  		b[2] = (byte) ( ( size & 0x00FF0000 ) >>> 16 );
46  		b[1] = (byte) ( ( size & 0x0000FF00 ) >>> 8 );
47  		b[0] = (byte) ( size & 0x000000FF );
48  		buf.put(b);
49  		
50  		//Number of fields
51  		int listLength = fields.size();
52  		b = new byte[4];
53  		b[3] = (byte) ( ( listLength & 0xFF000000 ) >>> 24 );
54  		b[2] = (byte) ( ( listLength & 0x00FF0000 ) >>> 16 );
55  		b[1] = (byte) ( ( listLength & 0x0000FF00 ) >>> 8 );
56  		b[0] = (byte) ( listLength & 0x000000FF );
57  		buf.put( b );
58  		
59  		//Flags
60  		buf.put( new byte[] {0x00,0x00,0x00,(byte)0xA0} );
61  		//means: We have a header and a footer, this is the header
62  		
63  		//Reserved 8-bytes 0x00
64  		buf.put( new byte[] {0,0,0,0,0,0,0,0} );
65  		
66  		//Now each field is saved:
67  		Iterator it = fields.iterator();
68  		while(it.hasNext()) {
69  			buf.put((byte[]) it.next());
70  		}
71  		
72  		//APETAGEX------------------
73  		buf.put( "APETAGEX".getBytes() );  //APETAGEX
74  		
75  		//Version 2.0 (aka 2000)
76  		buf.put( new byte[] {(byte)0xD0,0x07,0x00,0x00} );
77  		
78  		//Tag size
79  		b = new byte[4];
80  		b[3] = (byte) ( ( size & 0xFF000000 ) >>> 24 );
81  		b[2] = (byte) ( ( size & 0x00FF0000 ) >>> 16 );
82  		b[1] = (byte) ( ( size & 0x0000FF00 ) >>> 8 );
83  		b[0] = (byte) ( size & 0x000000FF );
84  		buf.put(b);
85  		
86  		//Number of fields
87  		b = new byte[4];
88  		b[3] = (byte) ( ( listLength & 0xFF000000 ) >>> 24 );
89  		b[2] = (byte) ( ( listLength & 0x00FF0000 ) >>> 16 );
90  		b[1] = (byte) ( ( listLength & 0x0000FF00 ) >>> 8 );
91  		b[0] = (byte) ( listLength & 0x000000FF );
92  		buf.put( b );
93  		
94  		//Flags
95  		buf.put( new byte[] {0x00,0x00,0x00,(byte)0x80} );
96  		//means: We have a header and a footer, this is the footer
97  		
98  		//Reserved 8-bytes 0x00
99  		buf.put( new byte[] {0,0,0,0,0,0,0,0} );
100 		
101 		//----------------------------------------------------------------------------
102 	}
103 	
104 	protected Tag getCompatibleTag(Tag tag) {
105 	    if(! (tag instanceof ApeTag)) {
106 		    ApeTag apeTag = new ApeTag();
107 		    apeTag.merge(tag);
108 		    return apeTag;
109 		}
110 	    return tag;
111 	}
112 	
113 	protected int getFixedTagLength(Tag tag) {
114 	    return 64;
115 	}
116 }