2 * Sonitus - DataPacket.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.data;
20 import static com.google.common.base.Preconditions.checkNotNull;
22 import com.google.common.base.Optional;
25 * A data packet is a container for audio data and optional metadata.
27 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29 public class DataPacket {
32 private final Optional<Metadata> metadata;
34 /** The audio data. */
35 private final byte[] buffer;
38 * Creates a new data packet.
41 * The metadata (may be {@code null})
45 public DataPacket(Metadata metadata, byte[] buffer) {
46 this(Optional.fromNullable(metadata), buffer);
50 * Creates a new data packet.
57 public DataPacket(Optional<Metadata> metadata, byte[] buffer) {
58 this.metadata = checkNotNull(metadata, "metadata must not be null");
59 this.buffer = checkNotNull(buffer, "buffer must not be null");
67 * Returns the metadata of this data packet.
69 * @return The metadata of this data packet
71 public Optional<Metadata> metadata() {
76 * Returns the audio data of this data packet.
78 * @return The audio data of this data packet
80 public byte[] buffer() {
89 public String toString() {
90 return String.format("%s (%d)", metadata, buffer.length);