From 2abb4d3033448cfb826c19f4d628685418ebb7cf Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 3 Jun 2013 22:24:37 +0200 Subject: [PATCH] Add identifier for FLAC streams. --- .../pterodactylus/sonitus/io/FlacIdentifier.java | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sonitus/io/FlacIdentifier.java diff --git a/src/main/java/net/pterodactylus/sonitus/io/FlacIdentifier.java b/src/main/java/net/pterodactylus/sonitus/io/FlacIdentifier.java new file mode 100644 index 0000000..5e01555 --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/io/FlacIdentifier.java @@ -0,0 +1,70 @@ +/* + * Sonitus - FlacIdentifier.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sonitus.io; + +import static net.pterodactylus.sonitus.io.flac.BlockType.STREAMINFO; + +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +import net.pterodactylus.sonitus.data.ContentMetadata; +import net.pterodactylus.sonitus.data.FormatMetadata; +import net.pterodactylus.sonitus.data.Metadata; +import net.pterodactylus.sonitus.io.flac.MetadataBlock; +import net.pterodactylus.sonitus.io.flac.Stream; +import net.pterodactylus.sonitus.io.flac.StreamInfo; + +import com.google.common.base.Optional; + +/** + * An identifier for FLAC input streams. + * + * @author David ‘Bombe’ Roden + */ +public class FlacIdentifier { + + /** + * Tries to identify the FLAC file contained in the given stream. + * + * @param inputStream + * The input stream + * @return The identified metadata, or {@link Optional#absent()} if the + * metadata can not be identified + * @throws IOException + * if an I/O error occurs + */ + public static Optional identify(InputStream inputStream) throws IOException { + Optional stream = Stream.parse(inputStream); + if (!stream.isPresent()) { + return Optional.absent(); + } + + List streamInfos = stream.get().metadataBlocks(STREAMINFO); + if (streamInfos.isEmpty()) { + /* FLAC file without STREAMINFO is invalid. */ + return Optional.absent(); + } + + MetadataBlock streamInfoBlock = streamInfos.get(0); + StreamInfo streamInfo = (StreamInfo) streamInfoBlock.data(); + + return Optional.of(new Metadata(new FormatMetadata(streamInfo.numberOfChannels(), streamInfo.sampleRate(), "FLAC"), new ContentMetadata())); + } + +} -- 2.7.4