Add parser for FLAC metadata.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / io / flac / Header.java
1 /*
2  * Sonitus - Header.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
25 /**
26  * Header for a {@link MetadataBlock}.
27  *
28  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29  */
30 public class Header {
31
32         /** Whether this metadata block is the last metadata block. */
33         private final boolean lastMetadataBlock;
34
35         /** The type of the metadata block. */
36         private final BlockType blockType;
37
38         /** The length of the metadata block. */
39         private final int length;
40
41         /**
42          * Creates a new metadata block header.
43          *
44          * @param lastMetadataBlock
45          *              {@code true} if this metadata block is the last metadata block in the FLAC
46          *              stream, {@code false} otherwise
47          * @param blockType
48          *              The type of the metadata block
49          * @param length
50          *              The length of the metadata block
51          */
52         private Header(boolean lastMetadataBlock, BlockType blockType, int length) {
53                 this.lastMetadataBlock = lastMetadataBlock;
54                 this.blockType = blockType;
55                 this.length = length;
56         }
57
58         //
59         // ACCESSORS
60         //
61
62         /**
63          * Returns whether this metadata block is the last metadata block in the FLAC
64          * {@link Stream}.
65          *
66          * @return {@code true} if this metadata block is last metadata block in the
67          *         FLAC stream, {@code false} otherwise
68          */
69         public boolean isLastMetadataBlock() {
70                 return lastMetadataBlock;
71         }
72
73         /**
74          * Returns the type of the metadata block.
75          *
76          * @return The type of the metadata block
77          */
78         public BlockType blockType() {
79                 return blockType;
80         }
81
82         /**
83          * Returns the length of the metadata block.
84          *
85          * @return The length of the metadata block
86          */
87         public int length() {
88                 return length;
89         }
90
91         //
92         // STATIC METHODS
93         //
94
95         /**
96          * Parses a metadata block header from the current position of the given input
97          * stream.
98          *
99          * @param inputStream
100          *              The input stream to parse the header from
101          * @return The parsed header
102          * @throws IOException
103          *              if an I/O error occurs
104          */
105         public static Header parse(InputStream inputStream) throws IOException {
106                 byte[] buffer = new byte[4];
107                 readFully(inputStream, buffer);
108                 boolean lastMetadataBlock = ((buffer[0] >> 7) & 0x01) != 0;
109                 BlockType blockType = BlockType.valueOf(buffer[0] & 0x7f);
110                 int length = ((buffer[1] & 0xff) << 16) | ((buffer[2] & 0xff) << 8) | (buffer[3] & 0xff);
111                 return new Header(lastMetadataBlock, blockType, length);
112         }
113
114 }