2 * Sonitus - Header.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sonitus.io.flac;
20 import static com.google.common.io.ByteStreams.readFully;
22 import java.io.IOException;
23 import java.io.InputStream;
26 * Header for a {@link MetadataBlock}.
28 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 /** Whether this metadata block is the last metadata block. */
33 private final boolean lastMetadataBlock;
35 /** The type of the metadata block. */
36 private final BlockType blockType;
38 /** The length of the metadata block. */
39 private final int length;
42 * Creates a new metadata block header.
44 * @param lastMetadataBlock
45 * {@code true} if this metadata block is the last metadata block in the FLAC
46 * stream, {@code false} otherwise
48 * The type of the metadata block
50 * The length of the metadata block
52 private Header(boolean lastMetadataBlock, BlockType blockType, int length) {
53 this.lastMetadataBlock = lastMetadataBlock;
54 this.blockType = blockType;
63 * Returns whether this metadata block is the last metadata block in the FLAC
66 * @return {@code true} if this metadata block is last metadata block in the
67 * FLAC stream, {@code false} otherwise
69 public boolean isLastMetadataBlock() {
70 return lastMetadataBlock;
74 * Returns the type of the metadata block.
76 * @return The type of the metadata block
78 public BlockType blockType() {
83 * Returns the length of the metadata block.
85 * @return The length of the metadata block
96 * Parses a metadata block header from the current position of the given input
100 * The input stream to parse the header from
101 * @return The parsed header
102 * @throws IOException
103 * if an I/O error occurs
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);