2 * Sonitus - Mp3Parser.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sonitus.io.mp3;
20 import java.io.EOFException;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import com.google.common.base.Optional;
25 import com.google.common.io.ByteStreams;
28 * A parser for MP3 files. It can recognize (and skip) ID3v2 header tags and
31 * This uses information from <a href="http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm">mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm</a>.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 /** The input stream to parse. */
38 private final InputStream inputStream;
40 /** The complete ID3v2 tag. */
41 private final byte[] id3Tag;
43 /** The current read buffer. */
44 private final byte[] buffer = new byte[4];
47 * Creates a new parser.
50 * The input stream to parse
52 * if an I/O error occurs
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);
72 * Returns the ID3v2 tag.
74 * @return The ID3v2 tag, or {@link Optional#absent()} if there is no ID3v2
77 public Optional<byte[]> getId3Tag() {
78 return Optional.fromNullable(id3Tag);
82 * Returns the next frame.
84 * @return The next frame
86 * if an I/O error occurs, or EOF is reached
88 public Frame nextFrame() throws IOException {
90 int r = inputStream.read();
92 throw new EOFException();
94 System.arraycopy(buffer, 1, buffer, 0, 3);
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()) {
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.
120 * The input stream to read from
122 * The buffer in which to read
124 * The offset at which to start writing into the buffer
126 * The amount of bytes to read
127 * @throws IOException
128 * if an I/O error occurs, or EOF is reached
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();