~ubuntu-branches/ubuntu/utopic/libjaudiotagger-java/utopic

« back to all changes in this revision

Viewing changes to src/org/jaudiotagger/audio/flac/FlacStreamReader.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-04-28 23:52:43 UTC
  • mfrom: (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110428235243-pzalvw6lncis3ukf
Tags: 2.0.3-1
* d/control: Drop Depends on default-jre per Debian Java Policy as its
  a library package.
* d/watch: Fix to directly monitor SVN tags.
* Switch to 3.0 (quilt) format.
* Bump Standards-Version to 3.9.2 (no changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.jaudiotagger.audio.flac;
2
 
 
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;
9
 
 
10
 
import java.io.IOException;
11
 
import java.io.RandomAccessFile;
12
 
import java.nio.ByteBuffer;
13
 
 
14
 
/**
15
 
 * Flac Stream
16
 
 * <p/>
17
 
 * Reader files and identifies if this is in fact a flac stream
18
 
 */
19
 
public class FlacStreamReader
20
 
{
21
 
    public static final int FLAC_STREAM_IDENTIFIER_LENGTH = 4;
22
 
    public static final String FLAC_STREAM_IDENTIFIER = "fLaC";
23
 
 
24
 
    private RandomAccessFile raf;
25
 
    private int startOfFlacInFile;
26
 
 
27
 
    /**
28
 
     * Create instance for holding stream info
29
 
     * @param raf
30
 
     */
31
 
    public FlacStreamReader(RandomAccessFile raf)
32
 
    {
33
 
        this.raf = raf;
34
 
 
35
 
    }
36
 
 
37
 
    /**
38
 
     * Reads the stream block to ensure it is a flac file
39
 
     *
40
 
     * @throws IOException
41
 
     * @throws CannotReadException
42
 
     */
43
 
    public void findStream() throws IOException, CannotReadException
44
 
    {
45
 
        //Begins tag parsing
46
 
        if (raf.length() == 0)
47
 
        {
48
 
            //Empty File
49
 
            throw new CannotReadException("Error: File empty");
50
 
        }
51
 
        raf.seek(0);
52
 
 
53
 
        //FLAC Stream at start
54
 
        if (isFlacHeader())
55
 
        {
56
 
            startOfFlacInFile = 0;
57
 
            return;
58
 
        }
59
 
 
60
 
        //Ok maybe there is an ID3v24tag first
61
 
        if (isId3v24Tag())
62
 
        {
63
 
            startOfFlacInFile = (int) (raf.getFilePointer() - FLAC_STREAM_IDENTIFIER_LENGTH);
64
 
            return;
65
 
        }
66
 
 
67
 
        //Ok maybe there is an ID3v23tag first
68
 
        if (isId3v23Tag())
69
 
        {
70
 
            startOfFlacInFile = (int) (raf.getFilePointer() - FLAC_STREAM_IDENTIFIER_LENGTH);
71
 
            return;
72
 
        }
73
 
 
74
 
        //Ok maybe there is an ID3v22tag first
75
 
        if (isId3v22Tag())
76
 
        {
77
 
            startOfFlacInFile = (int) (raf.getFilePointer() - FLAC_STREAM_IDENTIFIER_LENGTH);
78
 
            return;
79
 
        }
80
 
        throw new CannotReadException(ErrorMessage.FLAC_NO_FLAC_HEADER_FOUND.getMsg());
81
 
    }
82
 
 
83
 
    private boolean isId3v24Tag() throws IOException
84
 
    {
85
 
        int id3tagsize;
86
 
        ID3v24Tag id3tag = new ID3v24Tag();
87
 
        ByteBuffer bb = ByteBuffer.allocate(AbstractID3v2Tag.TAG_HEADER_LENGTH);
88
 
        raf.seek(0);
89
 
        raf.getChannel().read(bb);
90
 
        if (id3tag.seek(bb))
91
 
        {
92
 
            id3tagsize = id3tag.readSize(bb);
93
 
            raf.seek(id3tagsize);
94
 
            //FLAC Stream immediately after end of id3 tag
95
 
            if (isFlacHeader())
96
 
            {
97
 
                return true;
98
 
            }
99
 
        }
100
 
        return false;
101
 
    }
102
 
 
103
 
    private boolean isId3v23Tag() throws IOException
104
 
    {
105
 
        int id3tagsize;
106
 
        ID3v23Tag id3tag = new ID3v23Tag();
107
 
        ByteBuffer bb = ByteBuffer.allocate(AbstractID3v2Tag.TAG_HEADER_LENGTH);
108
 
        raf.seek(0);
109
 
        raf.getChannel().read(bb);
110
 
        if (id3tag.seek(bb))
111
 
        {
112
 
            id3tagsize = id3tag.readSize(bb);
113
 
            raf.seek(id3tagsize);
114
 
            //FLAC Stream immediately after end of id3 tag
115
 
            if (isFlacHeader())
116
 
            {
117
 
                return true;
118
 
            }
119
 
        }
120
 
        return false;
121
 
    }
122
 
 
123
 
    private boolean isId3v22Tag() throws IOException
124
 
    {
125
 
        int id3tagsize;
126
 
        ID3v22Tag id3tag = new ID3v22Tag();
127
 
        ByteBuffer bb = ByteBuffer.allocate(AbstractID3v2Tag.TAG_HEADER_LENGTH);
128
 
        raf.seek(0);
129
 
        raf.getChannel().read(bb);
130
 
        if (id3tag.seek(bb))
131
 
        {
132
 
            id3tagsize = id3tag.readSize(bb);
133
 
            raf.seek(id3tagsize);
134
 
            //FLAC Stream immediately after end of id3 tag
135
 
            if (isFlacHeader())
136
 
            {
137
 
                return true;
138
 
            }
139
 
        }
140
 
        return false;
141
 
    }
142
 
 
143
 
    private boolean isFlacHeader() throws IOException
144
 
    {
145
 
        //FLAC Stream at start
146
 
        byte[] b = new byte[FLAC_STREAM_IDENTIFIER_LENGTH];
147
 
        raf.read(b);
148
 
        String flac = new String(b);
149
 
        return flac.equals(FLAC_STREAM_IDENTIFIER);
150
 
    }
151
 
 
152
 
    /**
153
 
     * Usually flac header is at start of file, but unofficially and ID3 tag is allowed at the start of the file.
154
 
     *
155
 
     * @return the start of the Flac within file
156
 
     */
157
 
    public int getStartOfFlacInFile()
158
 
    {
159
 
        return startOfFlacInFile;
160
 
    }
161
 
}
 
1
package org.jaudiotagger.audio.flac;
 
2
 
 
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;
 
9
 
 
10
import java.io.IOException;
 
11
import java.io.RandomAccessFile;
 
12
import java.nio.ByteBuffer;
 
13
 
 
14
/**
 
15
 * Flac Stream
 
16
 * <p/>
 
17
 * Reader files and identifies if this is in fact a flac stream
 
18
 */
 
19
public class FlacStreamReader
 
20
{
 
21
    public static final int FLAC_STREAM_IDENTIFIER_LENGTH = 4;
 
22
    public static final String FLAC_STREAM_IDENTIFIER = "fLaC";
 
23
 
 
24
    private RandomAccessFile raf;
 
25
    private int startOfFlacInFile;
 
26
 
 
27
    /**
 
28
     * Create instance for holding stream info
 
29
     * @param raf
 
30
     */
 
31
    public FlacStreamReader(RandomAccessFile raf)
 
32
    {
 
33
        this.raf = raf;
 
34
 
 
35
    }
 
36
 
 
37
    /**
 
38
     * Reads the stream block to ensure it is a flac file
 
39
     *
 
40
     * @throws IOException
 
41
     * @throws CannotReadException
 
42
     */
 
43
    public void findStream() throws IOException, CannotReadException
 
44
    {
 
45
        //Begins tag parsing
 
46
        if (raf.length() == 0)
 
47
        {
 
48
            //Empty File
 
49
            throw new CannotReadException("Error: File empty");
 
50
        }
 
51
        raf.seek(0);
 
52
 
 
53
        //FLAC Stream at start
 
54
        if (isFlacHeader())
 
55
        {
 
56
            startOfFlacInFile = 0;
 
57
            return;
 
58
        }
 
59
 
 
60
        //Ok maybe there is an ID3v24tag first
 
61
        if (isId3v24Tag())
 
62
        {
 
63
            startOfFlacInFile = (int) (raf.getFilePointer() - FLAC_STREAM_IDENTIFIER_LENGTH);
 
64
            return;
 
65
        }
 
66
 
 
67
        //Ok maybe there is an ID3v23tag first
 
68
        if (isId3v23Tag())
 
69
        {
 
70
            startOfFlacInFile = (int) (raf.getFilePointer() - FLAC_STREAM_IDENTIFIER_LENGTH);
 
71
            return;
 
72
        }
 
73
 
 
74
        //Ok maybe there is an ID3v22tag first
 
75
        if (isId3v22Tag())
 
76
        {
 
77
            startOfFlacInFile = (int) (raf.getFilePointer() - FLAC_STREAM_IDENTIFIER_LENGTH);
 
78
            return;
 
79
        }
 
80
        throw new CannotReadException(ErrorMessage.FLAC_NO_FLAC_HEADER_FOUND.getMsg());
 
81
    }
 
82
 
 
83
    private boolean isId3v24Tag() throws IOException
 
84
    {
 
85
        int id3tagsize;
 
86
        ID3v24Tag id3tag = new ID3v24Tag();
 
87
        ByteBuffer bb = ByteBuffer.allocate(AbstractID3v2Tag.TAG_HEADER_LENGTH);
 
88
        raf.seek(0);
 
89
        raf.getChannel().read(bb);
 
90
        if (id3tag.seek(bb))
 
91
        {
 
92
            id3tagsize = id3tag.readSize(bb);
 
93
            raf.seek(id3tagsize);
 
94
            //FLAC Stream immediately after end of id3 tag
 
95
            if (isFlacHeader())
 
96
            {
 
97
                return true;
 
98
            }
 
99
        }
 
100
        return false;
 
101
    }
 
102
 
 
103
    private boolean isId3v23Tag() throws IOException
 
104
    {
 
105
        int id3tagsize;
 
106
        ID3v23Tag id3tag = new ID3v23Tag();
 
107
        ByteBuffer bb = ByteBuffer.allocate(AbstractID3v2Tag.TAG_HEADER_LENGTH);
 
108
        raf.seek(0);
 
109
        raf.getChannel().read(bb);
 
110
        if (id3tag.seek(bb))
 
111
        {
 
112
            id3tagsize = id3tag.readSize(bb);
 
113
            raf.seek(id3tagsize);
 
114
            //FLAC Stream immediately after end of id3 tag
 
115
            if (isFlacHeader())
 
116
            {
 
117
                return true;
 
118
            }
 
119
        }
 
120
        return false;
 
121
    }
 
122
 
 
123
    private boolean isId3v22Tag() throws IOException
 
124
    {
 
125
        int id3tagsize;
 
126
        ID3v22Tag id3tag = new ID3v22Tag();
 
127
        ByteBuffer bb = ByteBuffer.allocate(AbstractID3v2Tag.TAG_HEADER_LENGTH);
 
128
        raf.seek(0);
 
129
        raf.getChannel().read(bb);
 
130
        if (id3tag.seek(bb))
 
131
        {
 
132
            id3tagsize = id3tag.readSize(bb);
 
133
            raf.seek(id3tagsize);
 
134
            //FLAC Stream immediately after end of id3 tag
 
135
            if (isFlacHeader())
 
136
            {
 
137
                return true;
 
138
            }
 
139
        }
 
140
        return false;
 
141
    }
 
142
 
 
143
    private boolean isFlacHeader() throws IOException
 
144
    {
 
145
        //FLAC Stream at start
 
146
        byte[] b = new byte[FLAC_STREAM_IDENTIFIER_LENGTH];
 
147
        raf.read(b);
 
148
        String flac = new String(b);
 
149
        return flac.equals(FLAC_STREAM_IDENTIFIER);
 
150
    }
 
151
 
 
152
    /**
 
153
     * Usually flac header is at start of file, but unofficially and ID3 tag is allowed at the start of the file.
 
154
     *
 
155
     * @return the start of the Flac within file
 
156
     */
 
157
    public int getStartOfFlacInFile()
 
158
    {
 
159
        return startOfFlacInFile;
 
160
    }
 
161
}