1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34 if ( raf.length()==0 ) {
35
36 throw new CannotReadException("Error: File empty");
37 }
38 raf.seek( 0 );
39
40
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
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
58 case MetadataBlockHeader.VORBIS_COMMENT : tag = handleVorbisComment(mbh, raf);
59 mbh = null;
60 return tag;
61
62
63 default : raf.seek(raf.getFilePointer()+mbh.getDataLength());
64 break;
65 }
66
67 isLastBlock = mbh.isLastBlock();
68 mbh = null;
69 }
70
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