Add identifier for FLAC streams.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 3 Jun 2013 20:24:37 +0000 (22:24 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 4 Jun 2013 04:21:45 +0000 (06:21 +0200)
src/main/java/net/pterodactylus/sonitus/io/FlacIdentifier.java [new file with mode: 0644]

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 (file)
index 0000000..5e01555
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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<Metadata> identify(InputStream inputStream) throws IOException {
+               Optional<Stream> stream = Stream.parse(inputStream);
+               if (!stream.isPresent()) {
+                       return Optional.absent();
+               }
+
+               List<MetadataBlock> 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()));
+       }
+
+}