Supply the frame’s content.
[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  * <p/>
31  * This uses information from <a href="http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm">mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm</a>.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class Parser {
36
37         /** The input stream to parse. */
38         private final InputStream inputStream;
39
40         /** The complete ID3v2 tag. */
41         private final byte[] id3Tag;
42
43         /** The current read buffer. */
44         private final byte[] buffer = new byte[4];
45
46         /**
47          * Creates a new parser.
48          *
49          * @param inputStream
50          *              The input stream to parse
51          * @throws IOException
52          *              if an I/O error occurs
53          */
54         public Parser(InputStream inputStream) throws IOException {
55                 this.inputStream = inputStream;
56                 readFully(inputStream, buffer, 1, 3);
57                 if ((buffer[1] == 'I') && (buffer[2] == 'D') && (buffer[3] == '3')) {
58                         readFully(inputStream, buffer, 1, 3);
59                         byte[] lengthBuffer = new byte[4];
60                         readFully(inputStream, lengthBuffer, 0, 4);
61                         int headerLength = (lengthBuffer[0] << 21) | (lengthBuffer[1] << 14) | (lengthBuffer[2] << 7) | lengthBuffer[3];
62                         id3Tag = new byte[headerLength + 10];
63                         System.arraycopy(new byte[] { 'I', 'D', '3', buffer[1], buffer[2], buffer[3], lengthBuffer[0], lengthBuffer[1], lengthBuffer[2], lengthBuffer[3] }, 0, id3Tag, 0, 10);
64                         readFully(inputStream, id3Tag, 10, headerLength);
65                         readFully(inputStream, buffer, 1, 3);
66                 } else {
67                         id3Tag = null;
68                 }
69         }
70
71         /**
72          * Returns the ID3v2 tag.
73          *
74          * @return The ID3v2 tag, or {@link Optional#absent()} if there is no ID3v2
75          *         tag
76          */
77         public Optional<byte[]> getId3Tag() {
78                 return Optional.fromNullable(id3Tag);
79         }
80
81         /**
82          * Returns the next frame.
83          *
84          * @return The next frame
85          * @throws IOException
86          *              if an I/O error occurs, or EOF is reached
87          */
88         public Frame nextFrame() throws IOException {
89                 while (true) {
90                         int r = inputStream.read();
91                         if (r == -1) {
92                                 throw new EOFException();
93                         }
94                         System.arraycopy(buffer, 1, buffer, 0, 3);
95                         buffer[3] = (byte) r;
96                         if (Frame.isFrame(buffer, 0, 4)) {
97                                 int frameLength = Frame.getFrameLength(buffer, 0);
98                                 if (frameLength != -1) {
99                                         byte[] content = new byte[frameLength + 4];
100                                         readFully(inputStream, content, 4, frameLength);
101                                         System.arraycopy(buffer, 0, content, 0, 4);
102                                         Optional<Frame> frame = Frame.create(content, 0, frameLength + 4);
103                                         if (frame.isPresent()) {
104                                                 return frame.get();
105                                         }
106                                 }
107                         }
108                 }
109         }
110
111         //
112         // STATIC METHODS
113         //
114
115         /**
116          * Reads exactly {@code length} bytes from the given input stream, throwing an
117          * {@link EOFException} if there are not enough bytes left in the stream.
118          *
119          * @param inputStream
120          *              The input stream to read from
121          * @param buffer
122          *              The buffer in which to read
123          * @param offset
124          *              The offset at which to start writing into the buffer
125          * @param length
126          *              The amount of bytes to read
127          * @throws IOException
128          *              if an I/O error occurs, or EOF is reached
129          */
130         private static void readFully(InputStream inputStream, byte[] buffer, int offset, int length) throws IOException {
131                 if (ByteStreams.read(inputStream, buffer, offset, length) < length) {
132                         throw new EOFException();
133                 }
134         }
135
136 }