2 * Sonitus - FlacIdentifier.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;
20 import static net.pterodactylus.sonitus.io.flac.BlockType.STREAMINFO;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.List;
26 import net.pterodactylus.sonitus.data.ContentMetadata;
27 import net.pterodactylus.sonitus.data.FormatMetadata;
28 import net.pterodactylus.sonitus.data.Metadata;
29 import net.pterodactylus.sonitus.io.flac.MetadataBlock;
30 import net.pterodactylus.sonitus.io.flac.Stream;
31 import net.pterodactylus.sonitus.io.flac.StreamInfo;
33 import com.google.common.base.Optional;
36 * An identifier for FLAC input streams.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class FlacIdentifier {
43 * Tries to identify the FLAC file contained in the given stream.
47 * @return The identified metadata, or {@link Optional#absent()} if the
48 * metadata can not be identified
50 * if an I/O error occurs
52 public static Optional<Metadata> identify(InputStream inputStream) throws IOException {
53 Optional<Stream> stream = Stream.parse(inputStream);
54 if (!stream.isPresent()) {
55 return Optional.absent();
58 List<MetadataBlock> streamInfos = stream.get().metadataBlocks(STREAMINFO);
59 if (streamInfos.isEmpty()) {
60 /* FLAC file without STREAMINFO is invalid. */
61 return Optional.absent();
64 MetadataBlock streamInfoBlock = streamInfos.get(0);
65 StreamInfo streamInfo = (StreamInfo) streamInfoBlock.data();
67 return Optional.of(new Metadata(new FormatMetadata(streamInfo.numberOfChannels(), streamInfo.sampleRate(), "FLAC"), new ContentMetadata()));