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