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.exceptions.*;
22  import entagged.audioformats.ogg.OggTag;
23  import entagged.audioformats.ogg.util.OggTagReader;
24  
25  import java.io.*;
26  
27  
28  public class FlacTagReader {
29  	
30  	private OggTagReader oggTagReader = new OggTagReader();
31  	
32  	public OggTag read( RandomAccessFile raf ) throws CannotReadException, IOException {
33  		//Begins tag parsing-------------------------------------
34  		if ( raf.length()==0 ) {
35  			//Empty File
36  			throw new CannotReadException("Error: File empty");
37  		}
38  		raf.seek( 0 );
39  
40  		//FLAC Header string
41  		byte[] b = new byte[4];
42  		raf.read(b);
43  		String flac = new String(b);
44  		if(!flac.equals("fLaC"))
45  			throw new CannotReadException("fLaC Header not found, not a flac file");
46  		
47  		OggTag tag = null;
48  		
49  		//Seems like we hava a valid stream
50  		boolean isLastBlock = false;
51  		while(!isLastBlock) {
52  			b = new byte[4];
53  			raf.read(b);
54  			MetadataBlockHeader mbh = new MetadataBlockHeader(b);
55  		
56  			switch(mbh.getBlockType()) {
57  				//We got a vorbis comment block, parse it
58  				case MetadataBlockHeader.VORBIS_COMMENT : 	tag = handleVorbisComment(mbh, raf);
59  															mbh = null;
60  															return tag; //We have it, so no need to go further
61  				
62  				//This is not a vorbis comment block, we skip to next block
63  				default : 	raf.seek(raf.getFilePointer()+mbh.getDataLength());
64  							break;
65  			}
66  
67  			isLastBlock = mbh.isLastBlock();
68  			mbh = null;
69  		}
70  		//FLAC not found...
71  		throw new CannotReadException("FLAC Tag could not be found or read..");
72  	}
73  	
74  	private OggTag handleVorbisComment(MetadataBlockHeader mbh, RandomAccessFile raf) throws IOException, CannotReadException {
75  		long oldPos = raf.getFilePointer();
76  		
77  		OggTag tag = oggTagReader.read(raf);
78  		
79  		long newPos = raf.getFilePointer();
80  		
81  		if(newPos - oldPos != mbh.getDataLength())
82  			throw new CannotReadException("Tag length do not match with flac comment data length");
83  		
84  		return tag;
85  	}
86  }
87