2 * Sonitus - Data.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 * Accessor type that can parse the contents of a {@link MetadataBlock}.
28 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 /** The content of the metadata block. */
33 private final byte[] content;
36 * Creates a new data accessor.
39 * The content of the metadata block
41 public Data(byte[] content) {
42 this.content = content;
50 * Returns the content of this metadata block.
52 * @return The content of the metadata block
54 public byte[] content() {
63 * Parses the given number of bits from the content of this metadata block,
64 * starting at the given byte offset and bit offset (bit 0 being the most
68 * The byte offset at which to start reading
70 * The bit offset at which to start reading
72 * The number of bits to parse (should be <= 64)
73 * @return The parsed bits
75 protected long parseBits(int byteOffset, int bitOffset, int numberOfBits) {
77 int currentByteOffset = byteOffset;
78 int currentBitOffset = bitOffset;
79 int bitsRemaining = numberOfBits;
81 while (bitsRemaining > 0) {
82 value = (value << Math.min(8, bitsRemaining)) | ((content[currentByteOffset] & (0xff >>> currentBitOffset)) >> (8 - currentBitOffset - Math.min(bitsRemaining, 8 - currentBitOffset)));
83 bitsRemaining -= Math.min(bitsRemaining, 8 - currentBitOffset);
96 * Creates a new data accessor from the given input stream.
99 * The input stream to read the contents of the metadata block from
101 * The type of the metadata block
103 * The length of the metadata block
104 * @return The parsed metadata block
105 * @throws IOException
106 * if an I/O error occurs
108 public static Data parse(InputStream inputStream, BlockType blockType, int length) throws IOException {
109 byte[] buffer = new byte[length];
110 readFully(inputStream, buffer);
111 return blockType.createData(buffer);