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