Transport metadata inline.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / DataPacket.java
1 /*
2  * Sonitus - DataPacket.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.data;
19
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import com.google.common.base.Optional;
23
24 /**
25  * A data packet is a container for audio data and optional metadata.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class DataPacket {
30
31         /** The metadata. */
32         private final Optional<Metadata> metadata;
33
34         /** The audio data. */
35         private final byte[] buffer;
36
37         /**
38          * Creates a new data packet.
39          *
40          * @param metadata
41          *              The metadata (may be {@code null})
42          * @param buffer
43          *              The audio data
44          */
45         public DataPacket(Metadata metadata, byte[] buffer) {
46                 this(Optional.fromNullable(metadata), buffer);
47         }
48
49         /**
50          * Creates a new data packet.
51          *
52          * @param metadata
53          *              The metadata
54          * @param buffer
55          *              The audio date
56          */
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");
60         }
61
62         //
63         // ACCESSORS
64         //
65
66         /**
67          * Returns the metadata of this data packet.
68          *
69          * @return The metadata of this data packet
70          */
71         public Optional<Metadata> metadata() {
72                 return metadata;
73         }
74
75         /**
76          * Returns the audio data of this data packet.
77          *
78          * @return The audio data of this data packet
79          */
80         public byte[] buffer() {
81                 return buffer;
82         }
83
84         //
85         // OBJECT METHODS
86         //
87
88         @Override
89         public String toString() {
90                 return String.format("%s (%d)", metadata, buffer.length);
91         }
92
93 }