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 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 /** The input stream to parse. */
36 private final InputStream inputStream;
38 /** The complete ID3v2 tag. */
39 private final byte[] id3Tag;
41 /** The current read buffer. */
42 private final byte[] buffer = new byte[4];
45 * Creates a new parser.
48 * The input stream to parse
50 * if an I/O error occurs
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);
70 * Returns the ID3v2 tag.
72 * @return The ID3v2 tag, or {@link Optional#absent()} if there is no ID3v2
75 public Optional<byte[]> getId3Tag() {
76 return Optional.fromNullable(id3Tag);
80 * Returns the next frame.
82 * @return The next frame
84 * if an I/O error occurs, or EOF is reached
86 public Frame nextFrame() throws IOException {
88 int r = inputStream.read();
90 throw new EOFException();
92 System.arraycopy(buffer, 1, buffer, 0, 3);
94 Optional<Frame> frame = Frame.create(buffer, 0, 4);
95 if (frame.isPresent()) {
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.
110 * The input stream to read from
112 * The buffer in which to read
114 * The offset at which to start writing into the buffer
116 * The amount of bytes to read
117 * @throws IOException
118 * if an I/O error occurs, or EOF is reached
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();