Add parser for FLAC metadata.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / io / flac / Stream.java
1 /*
2  * Sonitus - MetadataBlock.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sonitus.io.flac;
19
20 import static com.google.common.io.ByteStreams.readFully;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.List;
27
28 import com.google.common.base.Optional;
29 import com.google.common.base.Predicate;
30 import com.google.common.collect.FluentIterable;
31 import com.google.common.collect.Lists;
32
33 /**
34  * Parser and container for information about a FLAC stream.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class Stream {
39
40         /** The metadata blocks of the stream. */
41         private final List<MetadataBlock> metadataBlocks = Lists.newArrayList();
42
43         /**
44          * Creates a new FLAC stream containing the given metadata blocks.
45          *
46          * @param metadataBlocks
47          *              The metadata blocks in order of appearance
48          */
49         private Stream(List<MetadataBlock> metadataBlocks) {
50                 this.metadataBlocks.addAll(metadataBlocks);
51         }
52
53         //
54         // ACCESSORS
55         //
56
57         /**
58          * Returns all metadata blocks of the given block type, in the order they
59          * appear in this stream.
60          *
61          * @param blockType
62          *              The block type to get all metadata blocks for
63          * @return The metadata blocks of the given block type
64          */
65         public Collection<MetadataBlock> metadataBlocks(final BlockType blockType) {
66                 return FluentIterable.from(metadataBlocks).filter(new Predicate<MetadataBlock>() {
67
68                         @Override
69                         public boolean apply(MetadataBlock metadataBlock) {
70                                 return metadataBlock.header().blockType() == blockType;
71                         }
72                 }).toList();
73         }
74
75         //
76         // STATIC METHODS
77         //
78
79         /**
80          * Parses the given input stream and returns information about the stream if it
81          * can be successfully parsed as a FLAC stream.
82          *
83          * @param inputStream
84          *              The input stream containing the FLAC stream
85          * @return The parsed FLAC stream, or {@link Optional#absent()} if no FLAC
86          *         stream could be found at the stream’s current position
87          * @throws IOException
88          *              if an I/O error occurs
89          */
90         public static Optional<Stream> parse(InputStream inputStream) throws IOException {
91                 byte[] streamTag = new byte[4];
92                 readFully(inputStream, streamTag);
93                 if (!Arrays.equals(streamTag, new byte[] { 'f', 'L', 'a', 'C' })) {
94                         return Optional.absent();
95                 }
96                 List<MetadataBlock> metadataBlocks = Lists.newArrayList();
97                 while (true) {
98                         MetadataBlock metadataBlock = MetadataBlock.parse(inputStream);
99                         metadataBlocks.add(metadataBlock);
100                         if (metadataBlock.header().isLastMetadataBlock()) {
101                                 break;
102                         }
103                 }
104                 return Optional.of(new Stream(metadataBlocks));
105         }
106
107 }