Add custom MP3 parser.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / io / mp3 / Parser.java
1 /*
2  * Sonitus - Mp3Parser.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sonitus.io.mp3;
19
20 import java.io.EOFException;
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 import com.google.common.base.Optional;
25 import com.google.common.io.ByteStreams;
26
27 /**
28  * A parser for MP3 files. It can recognize (and skip) ID3v2 header tags and
29  * MPEG audio frames.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class Parser {
34
35         /** The input stream to parse. */
36         private final InputStream inputStream;
37
38         /** The complete ID3v2 tag. */
39         private final byte[] id3Tag;
40
41         /** The current read buffer. */
42         private final byte[] buffer = new byte[4];
43
44         /**
45          * Creates a new parser.
46          *
47          * @param inputStream
48          *              The input stream to parse
49          * @throws IOException
50          *              if an I/O error occurs
51          */
52         public Parser(InputStream inputStream) throws IOException {
53                 this.inputStream = inputStream;
54                 readFully(inputStream, buffer, 0, 3);
55                 if ((buffer[0] == 'I') && (buffer[1] == 'D') && (buffer[2] == '3')) {
56                         readFully(inputStream, buffer, 0, 3);
57                         byte[] lengthBuffer = new byte[4];
58                         readFully(inputStream, lengthBuffer, 0, 4);
59                         int headerLength = (lengthBuffer[0] << 21) | (lengthBuffer[1] << 14) | (lengthBuffer[2] << 7) | lengthBuffer[3];
60                         id3Tag = new byte[headerLength + 10];
61                         System.arraycopy(new byte[] { 'I', 'D', '3', buffer[0], buffer[1], buffer[2], lengthBuffer[0], lengthBuffer[1], lengthBuffer[2], lengthBuffer[3] }, 0, id3Tag, 0, 10);
62                         readFully(inputStream, id3Tag, 10, headerLength);
63                         readFully(inputStream, buffer, 0, 3);
64                 } else {
65                         id3Tag = null;
66                 }
67         }
68
69         /**
70          * Returns the ID3v2 tag.
71          *
72          * @return The ID3v2 tag, or {@link Optional#absent()} if there is no ID3v2
73          *         tag
74          */
75         public Optional<byte[]> getId3Tag() {
76                 return Optional.fromNullable(id3Tag);
77         }
78
79         /**
80          * Returns the next frame.
81          *
82          * @return The next frame
83          * @throws IOException
84          *              if an I/O error occurs, or EOF is reached
85          */
86         public Frame nextFrame() throws IOException {
87                 while (true) {
88                         int r = inputStream.read();
89                         if (r == -1) {
90                                 throw new EOFException();
91                         }
92                         System.arraycopy(buffer, 1, buffer, 0, 3);
93                         buffer[3] = (byte) r;
94                         Optional<Frame> frame = Frame.create(buffer, 0, 4);
95                         if (frame.isPresent()) {
96                                 return frame.get();
97                         }
98                 }
99         }
100
101         //
102         // STATIC METHODS
103         //
104
105         /**
106          * Reads exactly {@code length} bytes from the given input stream, throwing an
107          * {@link EOFException} if there are not enough bytes left in the stream.
108          *
109          * @param inputStream
110          *              The input stream to read from
111          * @param buffer
112          *              The buffer in which to read
113          * @param offset
114          *              The offset at which to start writing into the buffer
115          * @param length
116          *              The amount of bytes to read
117          * @throws IOException
118          *              if an I/O error occurs, or EOF is reached
119          */
120         private static void readFully(InputStream inputStream, byte[] buffer, int offset, int length) throws IOException {
121                 if (ByteStreams.read(inputStream, buffer, offset, length) < length) {
122                         throw new EOFException();
123                 }
124         }
125
126 }