1
package org.jaudiotagger.audio.flac;
3
import org.jaudiotagger.audio.exceptions.CannotReadException;
4
import org.jaudiotagger.logging.ErrorMessage;
5
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
6
import org.jaudiotagger.tag.id3.ID3v22Tag;
7
import org.jaudiotagger.tag.id3.ID3v23Tag;
8
import org.jaudiotagger.tag.id3.ID3v24Tag;
10
import java.io.IOException;
11
import java.io.RandomAccessFile;
12
import java.nio.ByteBuffer;
17
* Reader files and identifies if this is in fact a flac stream
19
public class FlacStreamReader
21
public static final int FLAC_STREAM_IDENTIFIER_LENGTH = 4;
22
public static final String FLAC_STREAM_IDENTIFIER = "fLaC";
24
private RandomAccessFile raf;
25
private int startOfFlacInFile;
28
* Create instance for holding stream info
31
public FlacStreamReader(RandomAccessFile raf)
38
* Reads the stream block to ensure it is a flac file
41
* @throws CannotReadException
43
public void findStream() throws IOException, CannotReadException
46
if (raf.length() == 0)
49
throw new CannotReadException("Error: File empty");
53
//FLAC Stream at start
56
startOfFlacInFile = 0;
60
//Ok maybe there is an ID3v24tag first
63
startOfFlacInFile = (int) (raf.getFilePointer() - FLAC_STREAM_IDENTIFIER_LENGTH);
67
//Ok maybe there is an ID3v23tag first
70
startOfFlacInFile = (int) (raf.getFilePointer() - FLAC_STREAM_IDENTIFIER_LENGTH);
74
//Ok maybe there is an ID3v22tag first
77
startOfFlacInFile = (int) (raf.getFilePointer() - FLAC_STREAM_IDENTIFIER_LENGTH);
80
throw new CannotReadException(ErrorMessage.FLAC_NO_FLAC_HEADER_FOUND.getMsg());
83
private boolean isId3v24Tag() throws IOException
86
ID3v24Tag id3tag = new ID3v24Tag();
87
ByteBuffer bb = ByteBuffer.allocate(AbstractID3v2Tag.TAG_HEADER_LENGTH);
89
raf.getChannel().read(bb);
92
id3tagsize = id3tag.readSize(bb);
94
//FLAC Stream immediately after end of id3 tag
103
private boolean isId3v23Tag() throws IOException
106
ID3v23Tag id3tag = new ID3v23Tag();
107
ByteBuffer bb = ByteBuffer.allocate(AbstractID3v2Tag.TAG_HEADER_LENGTH);
109
raf.getChannel().read(bb);
112
id3tagsize = id3tag.readSize(bb);
113
raf.seek(id3tagsize);
114
//FLAC Stream immediately after end of id3 tag
123
private boolean isId3v22Tag() throws IOException
126
ID3v22Tag id3tag = new ID3v22Tag();
127
ByteBuffer bb = ByteBuffer.allocate(AbstractID3v2Tag.TAG_HEADER_LENGTH);
129
raf.getChannel().read(bb);
132
id3tagsize = id3tag.readSize(bb);
133
raf.seek(id3tagsize);
134
//FLAC Stream immediately after end of id3 tag
143
private boolean isFlacHeader() throws IOException
145
//FLAC Stream at start
146
byte[] b = new byte[FLAC_STREAM_IDENTIFIER_LENGTH];
148
String flac = new String(b);
149
return flac.equals(FLAC_STREAM_IDENTIFIER);
153
* Usually flac header is at start of file, but unofficially and ID3 tag is allowed at the start of the file.
155
* @return the start of the Flac within file
157
public int getStartOfFlacInFile()
159
return startOfFlacInFile;
1
package org.jaudiotagger.audio.flac;
3
import org.jaudiotagger.audio.exceptions.CannotReadException;
4
import org.jaudiotagger.logging.ErrorMessage;
5
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
6
import org.jaudiotagger.tag.id3.ID3v22Tag;
7
import org.jaudiotagger.tag.id3.ID3v23Tag;
8
import org.jaudiotagger.tag.id3.ID3v24Tag;
10
import java.io.IOException;
11
import java.io.RandomAccessFile;
12
import java.nio.ByteBuffer;
17
* Reader files and identifies if this is in fact a flac stream
19
public class FlacStreamReader
21
public static final int FLAC_STREAM_IDENTIFIER_LENGTH = 4;
22
public static final String FLAC_STREAM_IDENTIFIER = "fLaC";
24
private RandomAccessFile raf;
25
private int startOfFlacInFile;
28
* Create instance for holding stream info
31
public FlacStreamReader(RandomAccessFile raf)
38
* Reads the stream block to ensure it is a flac file
41
* @throws CannotReadException
43
public void findStream() throws IOException, CannotReadException
46
if (raf.length() == 0)
49
throw new CannotReadException("Error: File empty");
53
//FLAC Stream at start
56
startOfFlacInFile = 0;
60
//Ok maybe there is an ID3v24tag first
63
startOfFlacInFile = (int) (raf.getFilePointer() - FLAC_STREAM_IDENTIFIER_LENGTH);
67
//Ok maybe there is an ID3v23tag first
70
startOfFlacInFile = (int) (raf.getFilePointer() - FLAC_STREAM_IDENTIFIER_LENGTH);
74
//Ok maybe there is an ID3v22tag first
77
startOfFlacInFile = (int) (raf.getFilePointer() - FLAC_STREAM_IDENTIFIER_LENGTH);
80
throw new CannotReadException(ErrorMessage.FLAC_NO_FLAC_HEADER_FOUND.getMsg());
83
private boolean isId3v24Tag() throws IOException
86
ID3v24Tag id3tag = new ID3v24Tag();
87
ByteBuffer bb = ByteBuffer.allocate(AbstractID3v2Tag.TAG_HEADER_LENGTH);
89
raf.getChannel().read(bb);
92
id3tagsize = id3tag.readSize(bb);
94
//FLAC Stream immediately after end of id3 tag
103
private boolean isId3v23Tag() throws IOException
106
ID3v23Tag id3tag = new ID3v23Tag();
107
ByteBuffer bb = ByteBuffer.allocate(AbstractID3v2Tag.TAG_HEADER_LENGTH);
109
raf.getChannel().read(bb);
112
id3tagsize = id3tag.readSize(bb);
113
raf.seek(id3tagsize);
114
//FLAC Stream immediately after end of id3 tag
123
private boolean isId3v22Tag() throws IOException
126
ID3v22Tag id3tag = new ID3v22Tag();
127
ByteBuffer bb = ByteBuffer.allocate(AbstractID3v2Tag.TAG_HEADER_LENGTH);
129
raf.getChannel().read(bb);
132
id3tagsize = id3tag.readSize(bb);
133
raf.seek(id3tagsize);
134
//FLAC Stream immediately after end of id3 tag
143
private boolean isFlacHeader() throws IOException
145
//FLAC Stream at start
146
byte[] b = new byte[FLAC_STREAM_IDENTIFIER_LENGTH];
148
String flac = new String(b);
149
return flac.equals(FLAC_STREAM_IDENTIFIER);
153
* Usually flac header is at start of file, but unofficially and ID3 tag is allowed at the start of the file.
155
* @return the start of the Flac within file
157
public int getStartOfFlacInFile()
159
return startOfFlacInFile;