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.flac.util;
20  
21  import entagged.audioformats.ogg.util.OggTagCreator;
22  import entagged.audioformats.*;
23  
24  import java.io.UnsupportedEncodingException;
25  import java.nio.*;
26  
27  public class FlacTagCreator {
28  	
29  	public static final int DEFAULT_PADDING = 4000;
30  	private static final OggTagCreator creator = new OggTagCreator();
31  	
32  	//Creates the ByteBuffer for the ogg tag
33  	public ByteBuffer convert(Tag tag, int paddingSize) throws UnsupportedEncodingException {
34  		ByteBuffer ogg = creator.convert(tag);
35  		int tagLength = ogg.capacity() + 4;
36  		
37  		ByteBuffer buf = ByteBuffer.allocate( tagLength + paddingSize );
38  
39  		//CREATION OF CVORBIS COMMENT METADATA BLOCK HEADER
40  		//If we have padding, the comment is not the last block (bit[0] = 0)
41  		//If there is no padding, the comment is the last block (bit[0] = 1)
42  		byte type =  (paddingSize > 0) ? (byte)0x04 : (byte) 0x84;
43  		buf.put(type);
44  		int commentLength = tagLength - 4; //Comment length
45  		buf.put( new byte[] { (byte)((commentLength & 0xFF0000) >>> 16), (byte)((commentLength & 0xFF00) >>> 8) , (byte)(commentLength&0xFF)  } );
46  
47  		//The actual tag
48  		buf.put(ogg);
49  		
50  		//PADDING
51  		if(paddingSize >=4) {
52  			int paddingDataSize = paddingSize - 4;
53  			buf.put((byte)0x81); //Last frame, padding 0x81
54  			buf.put(new byte[]{ (byte)((paddingDataSize&0xFF0000)>>>16),(byte)((paddingDataSize&0xFF00)>>>8),(byte)(paddingDataSize&0xFF) });
55  			for(int i = 0; i< paddingDataSize; i++)
56  				buf.put((byte)0);
57  		}
58  		buf.rewind();
59  		
60  		return buf;
61  	}
62  	
63  	public int getTagLength(Tag tag) throws UnsupportedEncodingException {
64  		ByteBuffer ogg = creator.convert(tag);
65  		return ogg.capacity() + 4;
66  	}
67  }